Why we need a private constructor for std::initializer_list?

Viewed 274

The suggested implementation of std::initializer_list in the standard and The C++ Programming Language are simple. By simple I mean there is nothing strange.
But things become complex in the Compilers implementation of std::initializer_list, for example, GCC has a private constructor for std::initializer_list with a comment above it which says: 'The compiler can call a private constructor.'. Here eerorika answered:std::initializer_list is special. So I looked for it in the compilers source code:

Clang:

GCC:

And I don't understand why do we need to have a special private constructor? I encountered this some time ago when I wanted to convert std::vector<T> to std::initializer_list<T>.

1 Answers

The std::initializer_list is a reification of actual initializer-list, and its constructor is private simply to ensure no one is ever able to call it (except the compiler, of course), which prevents people from using std::initializer_list incorrectly.

As mentioned by StoryTeller; There is a gsl::span (or std::span in C++20) for contiguous range abstraction purpose. But it seems you try to (ab)use std::initializer_list for that instead.
(Which you can not as that is just a support type for {...} initialization.)


From comments:

... Couldn't it just be directly handled by the compiler itself? I mean something like a new keyword for it?

Well, a new keyword would potentially break existing codes, but the std namespace is a pretty safe place (for preventing conflicts).

Related