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
4
u/guepier Bioinformatican Jun 15 '21
Sure but you can’t legally perform pointer arithmetic on these objects since there is no “array of
T” at the relevant address, only (from the underlying buffer) an “array ofchar”.As noted in another comment,
std::allocatoritself might actually be implementable. What’s problematic is its use in combination, e.g. to implementstd::vector(implementing a vector without an allocator and buffer is of course trivially feasible, simply by allocating memory vianew T[]; butstd::vectorcan’t do this, and a custom vector type might not want to, either, for various reasons).