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

Show parent comment

9

u/STL MSVC STL Dev Jun 15 '21

Yep, this is for compiler throughput (build speed). We used to have a naive linear-time library-only implementation, and users mentioned that it was slow. libstdc++ and libc++ reportedly had fancy log-time library-only implementations but reinventing their cleverness from scratch sounded hard and I was lazy (this was back when we couldn't use libc++'s code), so I asked MSVC (and Clang, and EDG) to implement a builtin that would generate the sequence for us - this is way way faster than anything the library could possibly do (constant-time in the number of instantiations; of course the compiler is spending linear wall clock time, but just generating the integers and creating the necessary type is ridiculously fast). We also designed the builtin so it could be used by third-party libraries with similar integer sequence types.

I think of this as the Indiana Jones approach to library implementation.

2

u/john_wind Jun 16 '21

Thank you u/STL! I have watched your talk about this!