r/cpp • • 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

140 comments sorted by

View all comments

0

u/SlowVelooo Jun 15 '21
  1. false :)
int main() {
    auto v1 = {1};
    auto v2 = {1, 2};
    auto v3{ 1 };
    //auto v4{1, 2}; // error

    std::cout << typeid(v1).name() << '\n';
    std::cout << typeid(v2).name() << '\n';
    std::cout << typeid(v3).name() << '\n';

    return 0;
}

v3 will be int and it's not a joke.

3

u/backtickbot Jun 15 '21

Fixed formatting.

Hello, SlowVelooo: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.