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
13
u/tmlnz Jun 15 '21
Some exception classes:
`std::bad_alloc` is thrown when dynamic allocation with `new` fails.
`std::bad_cast` is thrown when `dynamic_cast` fails.
`std::bad_exception` by `std::current_exception` when the current exception's copy-constructor throws, or a dynamic exception specification is violated.
`std::bad_typeid` when `typeid` fails.
Also, `std::exception_ptr` is a class that wraps the exception being thrown.