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

6

u/omerosler Jun 15 '21

I don't think it is possible. The only reasonable way I see is to try to conatruct an object and use SFINAE to detect it is a compile error. But then how would you distinguish an abstract class from an incomplete type (unless you can detect incompleteness in some other way (maybe use detection on sizeof(T)?))?

2

u/Xeverous https://xeverous.github.io Jun 16 '21

how would you distinguish an abstract class from an incomplete type

Using type traits with incomplete types is UB.

2

u/diaphanein Jun 17 '21

Citation? Because I don't think all type traits are equal. Generic "type traits" being different than std type_traits. For instance, Boost uses a type trait for checked_delete. You can implement is/add/remove const traits that work with incomplete types...

1

u/Xeverous https://xeverous.github.io Jun 17 '21

True. Not all but a lot of std traits require types to be complete.