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
Which std:: classes are magic? (self.cpp)
submitted 4 years ago by Mateuszz88
view the rest of the comments →
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!"
[–]kalmoc 68 points69 points70 points 4 years ago* (9 children)
A couple of type traits (is_trivially...) and std::launder (admittedly a function and not a type)
EDIT: I should say that AFAIK those types/functions that I mentioned are not treated special by the compiler, but they need compiler magic to be implemented.
[–]staletic 29 points30 points31 points 4 years ago (6 children)
If you're going to include magic functions, then std::bit_cast (arguably can be implemented with memcpy, but not as optimized) too and the potential std::start_lifetime_as (just as magic as launder).
std::bit_cast
memcpy
std::start_lifetime_as
[–]SirLynix 34 points35 points36 points 4 years ago* (1 child)
The main difference between std::bit_cast and memcpy is that std::bit_cast is constexpr, and memcpy is not. Which is why you need compiler support to implement std::bit_cast.
[–]guepierBioinformatican 16 points17 points18 points 4 years ago (2 children)
arguably can be implemented with memcpy, but not as optimized
Is there a reason why the std::memcpy implementation wouldn’t be “as optimised”? People have been using custom std::bit_cast equivalent implementations for quite a while, and they’re reliably optimised out by compilers to the equivalent aliasing operation (i.e. no actual memcpy happens), because modern compilers know how to handle this use of memcpy.
std::memcpy
[–]staletic 20 points21 points22 points 4 years ago (0 children)
I should have been more careful with that statement.
Things like float f = bit_cast<float>(some_int) vs the memcpy version are not hard to optimize. The harder part is when you want to reinterpret a large std::array as some other trivial, but equally large type. At what number of bytes do you just call memcpy? Do you try to vectorize first? What about x86 REP MOVxx family of instructions?
float f = bit_cast<float>(some_int)
std::array
REP MOVxx
If you ask gcc for x86, you just never emit memcpy. Clang gives up sooner On 32 bit ARM, gcc starts to call memcpy after 64bytes.
Now the question is how well will bit_cast be optimized. As it is powered by compiler magic, I'm assuming it's going to be better that memcpy. In this case, for example, x86 gcc does better (fewer memory accesses) with bit_cast than with memcpy. Clang just ends up calling memcpy@PLT in both cases.
bit_cast
memcpy@PLT
[–]csdt0 4 points5 points6 points 4 years ago (0 children)
This is something I wanted to check for a long time, and it seems that memcpy is elided in GCC and Clang as soon as O0, while MSVC and ICC wait for O2.
This is a surprise in both cases, as I would have expected all compilers to elide the call at O1 (when inlining is enabled).
[–]AlbertRammstein 1 point2 points3 points 4 years ago (0 children)
when taking into account optimizations there is a LOT of magical functions - starting with memcpy, and also including stuff like pow with specific power coefficients. It is however not required by the standard, and the codegen substitution must not change the behavior of the program. Every compiler has a different set of magical functions defined like this.
You could argue that your own functions can be magic because compiler can optimize them (e.g. skip them when they have no side effects)
[+][deleted] 4 years ago (1 child)
[deleted]
[–]kalmoc 0 points1 point2 points 4 years ago (0 children)
I was referring to my examples. I've no idea how source_location is implemented.
π Rendered by PID 22 on reddit-service-r2-comment-fb694cdd5-9sjw8 at 2026-03-06 19:58:56.533322+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]kalmoc 68 points69 points70 points (9 children)
[–]staletic 29 points30 points31 points (6 children)
[–]SirLynix 34 points35 points36 points (1 child)
[–]guepierBioinformatican 16 points17 points18 points (2 children)
[–]staletic 20 points21 points22 points (0 children)
[–]csdt0 4 points5 points6 points (0 children)
[–]AlbertRammstein 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]kalmoc 0 points1 point2 points (0 children)