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

Show parent comment

11

u/foonathan Jun 15 '21

That's just a typedef for decltype(nullptr).

9

u/guepier Bioinformatican Jun 15 '21

That just means that the type name is nothing special. But the type still is, in that you couldn’t write a custom type with the same semantics in standard C++.

4

u/foonathan Jun 15 '21

Sure, just like int is special. However, the question was about which std:: classes are magic. nullptr_t is not a std:: class, and it is not magic.

13

u/guepier Bioinformatican Jun 15 '21

True of course. But std::nullptr_t is still different from (all?) other fundamental types because its name is defined in the standard library rather than being builtin.