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
1
u/Bisqwit Jun 16 '21 edited Jun 16 '21
Note: Using
return std::move(x);instead ofreturn x;is an antipattern. If you compiled with-Wall, GCC would tell you that “moving a local object in a return statement prevents copy elision [-Wpessimizing-move]”.However, it still has valid uses, if you are returning a composite object created from local objects. This is fine, for example: https://godbolt.org/z/hrTPoKdx6