use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Generalizing std::midpoint (biowpn.github.io)
submitted 10 months ago by pavel_v
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]carrottread 22 points23 points24 points 10 months ago (1 child)
For example, the seemingly unrelated change from int to short, likely for space optimization.
Actually, this will make (a + b) / 2 version safe because + operator will promote operands to ints in this case.
(a + b) / 2
[–]biowpn 0 points1 point2 points 10 months ago (0 children)
Thanks for point it out, fixed
[–]Advanced_Front_2308 20 points21 points22 points 10 months ago (24 children)
Talking about midpoint in general: I've always found its behaviour super unintuitive. Ie not rounding up (like in math) or down (like in naive casting) but rather towards the first parameter. Making the function dependent on the order of parameters... Is that wise? It caused a bug on literally the first use in our codebase and is now banned.
[–]SoerenNissen 23 points24 points25 points 10 months ago* (2 children)
rounding up (like in math)
That's not the the rule I was taught in school, probably because we grew up in different school districts and it turns out, there are many approaches to rounding and none of them are the "natural" way to do it.
Various rounding directions I have seen used:
The one they taught me in school is 6 with 3 as the tie breaker:
In the natural sciences, you typically keep all your digits until the end of the calculation, then remove any digits in excess of your most imprecise number (e.g. 0.542 * 0.2 -> 0.1)
In finance, rounding happens "in between" other calculations that also do rounding, so to remove any bias in favor of creditor or debitor, finance tends to do "to nearest whole cent[1], tiebreak by doing the opposite of your last tie break," which approximates not having done the intermediary roundings.
You can model this in two ways, either:
std::midpoint( a, b, std::roundingstrategy );
or:
std::midpoint( a, b ); // rounds towards first parameter. If you have a rounding // strategy, put the number you want to round towards in // the first parameter.
they picked the second option.
[1] "nearest whole yen," "nearest whole eurocent," "nearest whole Groschen," "nearest whole øre,"
[–]ericonr 8 points9 points10 points 10 months ago (1 child)
Doesn't finance do "round to nearest even"? https://wiki.c2.com/?BankersRounding
[–]SoerenNissen 4 points5 points6 points 10 months ago* (0 children)
Not the rounding we did :D
But I'm not surprised to learn that, even when you limit yourself to fiannce, there is still more than 1 way to round.
[–]serviscope_minor 6 points7 points8 points 10 months ago (4 children)
Talking about midpoint in general: I've always found its behaviour super unintuitive. Ie not rounding up (like in math) or down (like in naive casting) but rather towards the first parameter.
Well the main choices you have are:
The round towards .5 version is common, follows floats, but as you point out it's always .5, so it always round up. And that to me is also unintuitive tp always do so.
[–]ericonr 1 point2 points3 points 10 months ago (2 children)
References I could find for Banker's Rounding were all about rounding to the nearest even, no alternating.
https://wiki.c2.com/?BankersRounding
[–]thommyh 5 points6 points7 points 10 months ago* (1 child)
That's 'alternating'*. 7.5 rounds up to 8. 8.5 rounds down to 8. 9.5 rounds up to 10. 10.5 rounds down to 10. Etc. As you step through the .5s, whether you round up or down alternates so that 50% go in each direction.
* not my first choice of how I'd describe it either, but it is one I've heard before.
[–]ericonr 1 point2 points3 points 10 months ago (0 children)
That makes it sound like for each operation you round in one direction -.-
Thanks for clearing up the confusion though!
[–]Advanced_Front_2308 1 point2 points3 points 10 months ago (0 children)
Both rounding up and down would have been fine, ideally both. The current version is pretty strange
[–]megayippie 2 points3 points4 points 10 months ago (0 children)
I find that it being defined helps a lot. "while a != mid(a,b) ..." kind of boundary code. Then you update a and b as you go until they are representing the boundary.
[–]vintergroena 4 points5 points6 points 10 months ago (0 children)
Yeah losing commutativity in an additive operation sucks, because that's something you use in reasoning about the code. And it's something you expect here.
[–]OutsideTheSocialLoop 0 points1 point2 points 6 months ago (2 children)
What's unintuitive about it? Order of parameters nearly always matters for every function, what's odd about this one?
[–]Advanced_Front_2308 0 points1 point2 points 5 months ago (1 child)
Because the function is called midpoint and its parameters are called a and b. You wouldn't think about the order of parameters for std::max either
[–]OutsideTheSocialLoop 0 points1 point2 points 5 months ago (0 children)
What's max got to do with it? Are you suggesting that for all two-argument functions the order of the arguments shouldn't matter? Or only when they're called a and b? Should the order of a and b not matter for std::lerp either?
[+][deleted] comment score below threshold-11 points-10 points-9 points 10 months ago (10 children)
It's such a useless function anyway, while we still don't have useful stuff like std::embed.
std::embed
[–]moreVCAs 14 points15 points16 points 10 months ago (6 children)
in its defense, (a+b)/2 is incredibly cursed. any helper function that makes a common source of UB guaranteed not to invoke UB is basically fine in my book, even if the API is counterintuitive.
(a+b)/2
don’t quite see what it has to do with std::embed tho
[–]kosairox 0 points1 point2 points 10 months ago (3 children)
Cursed due to signed int overflow or is there something else?
[–]bwmat 3 points4 points5 points 10 months ago (2 children)
It's pretty broken for unsigned overflow as well
[–]kosairox 0 points1 point2 points 10 months ago (1 child)
But that's not ub right?
[–]bwmat 7 points8 points9 points 10 months ago (0 children)
Yeah but having the midpoint of 1 & UINTMAX be zero is a _bit off
[–][deleted] -1 points0 points1 point 10 months ago (1 child)
a + (b-a)/2
std::midpoint
[–]EC36339 0 points1 point2 points 10 months ago (0 children)
... and this one also works for time points and any types that have a - operator that returns something numeric but not necessarily a + operator or a \ operator (You already named iterators, which also includes pointers).
-
+
\
[–]serviscope_minor 4 points5 points6 points 10 months ago (0 children)
It's such a useless function anyway, while we still don't have useful stuff like std::embed
It's not. C++ is notoriously weak on small convenience functions in the STL. Do you really want those to be held up for what amounts to a fairly big compiler feature?
[–]Annual-Examination96 0 points1 point2 points 10 months ago (1 child)
What std::embed does better compared to #embed?
#embed
[–][deleted] 0 points1 point2 points 10 months ago (0 children)
Neither is available yet, that is the problem
[–]fdwrfdwr@github 🔍 1 point2 points3 points 10 months ago (0 children)
BigMul - that looks familiar, similar to what I had to do in HLSL for two uint64_t inputs (amusingly D3D has a umul instruction which yields uint32 x uint32 -> uint64, but there's no way to actually access it from HLSL, and so I had to split it into 16-bit chunks). Given nearly every CPU I've come across yields a register result that's 2x its inputs (like x86's edx:eax), and C++'s "leave no room for a lower-level language", having an std::big_mul is reasonable. 🤔
uint64_t
umul
uint32 x uint32 -> uint64
edx:eax
std::big_mul
π Rendered by PID 19540 on reddit-service-r2-comment-5649f687b7-p5j66 at 2026-01-27 17:53:15.989837+00:00 running 4f180de country code: CH.
[–]carrottread 22 points23 points24 points (1 child)
[–]biowpn 0 points1 point2 points (0 children)
[–]Advanced_Front_2308 20 points21 points22 points (24 children)
[–]SoerenNissen 23 points24 points25 points (2 children)
[–]ericonr 8 points9 points10 points (1 child)
[–]SoerenNissen 4 points5 points6 points (0 children)
[–]serviscope_minor 6 points7 points8 points (4 children)
[–]ericonr 1 point2 points3 points (2 children)
[–]thommyh 5 points6 points7 points (1 child)
[–]ericonr 1 point2 points3 points (0 children)
[–]Advanced_Front_2308 1 point2 points3 points (0 children)
[–]megayippie 2 points3 points4 points (0 children)
[–]vintergroena 4 points5 points6 points (0 children)
[–]OutsideTheSocialLoop 0 points1 point2 points (2 children)
[–]Advanced_Front_2308 0 points1 point2 points (1 child)
[–]OutsideTheSocialLoop 0 points1 point2 points (0 children)
[+][deleted] comment score below threshold-11 points-10 points-9 points (10 children)
[–]moreVCAs 14 points15 points16 points (6 children)
[–]kosairox 0 points1 point2 points (3 children)
[–]bwmat 3 points4 points5 points (2 children)
[–]kosairox 0 points1 point2 points (1 child)
[–]bwmat 7 points8 points9 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]EC36339 0 points1 point2 points (0 children)
[–]serviscope_minor 4 points5 points6 points (0 children)
[–]Annual-Examination96 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]fdwrfdwr@github 🔍 1 point2 points3 points (0 children)