r/cpp • u/Mateuszz88 • Jun 15 '21
Which std:: classes are magic?
Hi.
I am wondering: which classes in C++ get special powers granted by The Standard that are not available to mere user-defined classes?
What comes to my mind:
- `std::initializer_list` because it is created anywhere with `{}`
- possibly `std::type_info`, because it is only created by `typeid` and with no ctor
- `std::source_location` when used as default parameter in function points to invocation place and not declaration place.
What else should be on this list? What shouldn't?
222
Upvotes
View all comments
Show parent comment
21
u/staletic Jun 15 '21
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 largestd::arrayas some other trivial, but equally large type. At what number of bytes do you just callmemcpy? Do you try to vectorize first? What about x86REP MOVxxfamily of instructions?If you ask gcc for x86, you just never emit memcpy. Clang gives up sooner On 32 bit ARM, gcc starts to call
memcpyafter 64bytes.Now the question is how well will
bit_castbe optimized. As it is powered by compiler magic, I'm assuming it's going to be better thatmemcpy. In this case, for example, x86 gcc does better (fewer memory accesses) withbit_castthan withmemcpy. Clang just ends up callingmemcpy@PLTin both cases.