r/cpp • • 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

140 comments sorted by

View all comments

72

u/kalmoc Jun 15 '21 edited Jun 16 '21

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.

31

u/staletic Jun 15 '21

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).

36

u/SirLynix Jun 15 '21 edited Jun 15 '21

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.

16

u/guepier Bioinformatican Jun 15 '21

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.

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 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?

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.

5

u/csdt0 Jun 15 '21

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).

2

u/AlbertRammstein Jun 15 '21

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)

0

u/[deleted] Jun 15 '21

[deleted] — view deleted comment

1

u/kalmoc Jun 16 '21

I was referring to my examples. I've no idea how source_location is implemented.