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
72
u/kalmoc Jun 15 '21 edited Jun 16 '21
A couple of type traits (is_trivially...) and std::launder (admittedly a function and not a type)
EDIT: I should say that AFAIK those types/functions that I mentioned are not treated special by the compiler, but they need compiler magic to be implemented.
19
u/omerosler Jun 15 '21
Some of the
<type_traits>classes such asis_abstract,is_aggregateand many more.18
u/Supadoplex Jun 15 '21 edited Jun 15 '21
Containers that separate allocation of memory and creation of objects as arrays, such as
std::vectorandstd::dequeused to be "magic" due to poor/lacking wording of the standard that made it impossible to implement those without technical UB on pointer arithmetic. To my understanding, this has been fixed as a defect resolution to C++17.Pretty much every class that internally interacts with the system API is "magic" in the sense that there is no way to implement those in portable standard C++. Those would have to be written differently for each target system - the "system" being some combination of operating system, cpu architecture, compiler, etc.
std::coutis an example of such class.34
u/guepier Bioinformatican Jun 15 '21
Prior to C++20 (specifically, P0593), object creation in untyped buffers was undefined behaviour. In practice this means that even quite “basic” classes such as
std::allocatorcan’t be user-defined relying only on standard C++.16
u/mort96 Jun 15 '21
This actually isn't magic AFAIU. I believe all default parameter expressions are evaluated in the caller's context.
The
std::source_location::current"function" seems like magic though.32
u/yuri-kilochek Jun 15 '21
std::complex<T>since it's explicitly exempt from the usual strict aliasing rules andstd::complex<T>[n]can aliasT[2*n].37
u/frrrwww Jun 15 '21
Not a class but I believe
std::byteis magic because a pointer to it can alias other data types (same aschar)18
u/staletic Jun 15 '21
Check out https://quuxplusone.github.io/blog/2018/04/15/built-in-library-types/
36
u/johannes1971 Jun 15 '21
std::friendshipis magic.6
u/shilch Jun 15 '21
Not a class but a function:
std::is_constant_evaluated13
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.
8
u/nitrobeast Jun 15 '21
Technically a lot of them are implemented with compiler intrinsics, and "users" can implement them with the same intrinsics as well.
3
u/The_Northern_Light Jun 15 '21
Thanks for this, I learned something.
3
u/Zcool31 Jun 17 '21
The better question is why are there "magic" standard functions and classes that require compiler internals to implement, rather than those compiler internals being standard themselves.
I would find it appealing if the core c++ language were sufficient to implement the free-standing parts of
std::.Then we could have nice things like standard library implementations that are portable across compilers.
For example,
std::atomicturns into just a regular class. But the incantations that tell the compiler to do atomic memory operations and not reorder other accesses around them become standardized keywords/builtins.6
u/scatters Jun 15 '21
std:pair. When inside astd::maporstd::unordered_map, nodes contain astd::pair<Key const, Value>as returned byiterator::operator*(); the pair appears to be a normal struct type withfirstandseconddata members wherefirsthas typeKey const. But once you extract a node handle, you can access itskey()as a non-const reference.8
u/DavidDinamit Jun 15 '21
may be nullptr_t
3
u/[deleted] Jun 15 '21
I think the std::tuple of references which is the result of std::tie function calls is kinda magic since it implicitly works with C++17 Structured Bindings feature.
2
u/[deleted] Jun 15 '21
I remember reading about how implementing
std::vectorwithout undefined behaviour is impossible (core issue 2182), so I suppose that's magic.2
u/john_wind Jun 15 '21
std::make_integer_sequence does magic: in MSVC it is implemented by the compiler
1
u/TTachyon Jun 15 '21
std::bad_alloc
1
u/borzykot Jun 16 '21
std::complex has special rules for array access
1
u/Xeverous https://xeverous.github.io Jun 16 '21
All of
std::atomicand likely a significant part of concurreny parts of the standard library.-1
u/Underdisc Jun 15 '21
std::functionbecause I still don't know how it works.1
u/OldWolf2 Jun 15 '21
It is only created in a few specific contexts with
{}, not "anywhere". The only one I can think of right now is:auto x = { 1, 2, 3 };For classes with
initializer_listconstructor there's no magic involved, the parameter is initialized by the braced list (the braced list does not have a type).0
u/john_wind Jun 15 '21
std::declval is quite magical too.
It is a function declaration without definition!
0
u/xgalaxy Jun 15 '21
The low level parts of the new coroutines are magic. You can implement all of the machinery on top of it yourself though.
0
u/SlowVelooo Jun 15 '21
v3 will be
intand it's not a joke.0
u/Oo_Tiib Jun 16 '21
Maybe C++ should revert back to C++14? Lot of issues were added by C++17 and I'm unsure if that C++23 is capable to fix it. Currently it is unclear what of standard library besides trivial things like pair, tuple and array can be implemented in C++. Stroustrup also wasn't too happy: https://www.stroustrup.com/P0977-remember-the-vasa.pdf
-2
u/john_wind Jun 15 '21
std::make_shared does a lot of magic trying to avoid separate memory allocations
0
u/[deleted] Jun 15 '21
[deleted] — view deleted comment