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
8
u/guepier Bioinformatican Jun 15 '21
The example given isn’t that somebody doesn’t “know” it’s an
enum. The point is that it’s conceptually wrong. It pushes something that should be a hidden implementation detail —std::bytehappens to be implemented as an enum class — into the public API.This violates all kinds of software engineering principles about writing good abstractions.
And the specific example somebody gave is if a library uses e.g.
std::is_enumto dispatch formatted output. Doing this would be an entirely reasonable design decision, but because ofstd::byte’s implementation, this decision is broken unless a special case forstd::byteis introduced.Forcing third-party code to introduce special cases for types that should be handle-able uniformly is certainly bad design (and entirely avoidable). And, just to re-emphasise the point: this has nothing to do with user’s ignorance! A competent developer still has to introduce a special case that shouldn’t exist.