I want to make a small library of my own containers without relying on STL at all, as a form of exercise. How can I define an initializer list constructor for my classes without std::initializer_list? How is it even implemented?
Asked
Active
Viewed 415 times
1
-
1Long story short - you can't. `std::vector` couldn't, you can't do it general case either. There is a reason why `std::initializer_list` exists. – Aykhan Hagverdili Mar 09 '20 at 17:18
-
Well that sucks – Big Temp Mar 09 '20 at 17:19
-
Because of the way the was the language works `{ stuff }` doesn't actually have a type. It takes the compiler to do some "magic" to turn that *braced-init-list* into a `std::initializer_list`. You would have to make your own compiler to provide your own "magic". – NathanOliver Mar 09 '20 at 17:20
-
Does this answer your question? [Implementation of std::initializer\_list](https://stackoverflow.com/questions/18164353/implementation-of-stdinitializer-list) – Aykhan Hagverdili Mar 09 '20 at 17:23
-
1`
` is guaranteed to be available on freestanding implementations, so there is no reason to avoid it. – Brian Bi Mar 09 '20 at 17:23 -
An "initializer list constructor" *implies* use of `std::initializer_list`. Yet you want to *not* use `std::initializer_list`? Sorry. Your question makes no sense to me. – Jesper Juhl Mar 09 '20 at 17:37
-
"want to make a small library of my own containers without relying on STL at all" - Why? Why would you *not* take advantage of the library that has already been developed and debugged by experts and is available with every C++ implementation and instead develop your own library that *almost certainly* will be inferior? Why? Sounds like a enormous waste of time to me. – Jesper Juhl Mar 09 '20 at 19:27
-
@JesperJuhl You seem to have missed those few words after a comma – Big Temp Mar 09 '20 at 20:12
-
@Big yes, you are right. I did miss that. As an exercise it's obviously OK. Just don't do that for production code.. – Jesper Juhl Mar 09 '20 at 20:15
-
@JesperJuhl I won't, I promise – Big Temp Mar 09 '20 at 20:54
1 Answers
3
How can I define an initializer list constructor for my classes without
std::initializer_list
?
You don't. The very definition of "initializer list constructor" is tied into std::initializer_list
. Just like the result of typeid
is tied directly into std::type_info
, and the result of sizeof
and alignof
is a std::size_t
.
Attempting to use C++ while pretending the standard library in its entirety does not exist is folly. There are parts of it you can ignore without consequence, but if you want to have an "initializer list constructor", you have to use std::initializer_list
.
The C++ standard library is not optional; it is not divorced from C++ as a langauge. Parts of it are, but std::initializer_list
isn't one of those parts.

Nicol Bolas
- 449,505
- 63
- 781
- 982
-
Why did they have to invent something called `initalizer_list` instead of reusing `std::vector` or `std::array`? Something like `{ 1, 2, 3 }` means `std::vector
` which contains 1, 2 and 3? – Aykhan Hagverdili Mar 09 '20 at 17:36 -
2@Ayxan because `std::vector
` implies dynamic allocation, and `std::array – Caleth Mar 09 '20 at 17:39` ties the number of elements into the type. `std::initalizer_list` is a temporary with unspecified size -
-
1@Ayxan: `std::initializer_list` is just a pair of pointers. The temporary it refers to is a temporary. – Nicol Bolas Mar 09 '20 at 17:40
-
@NicolBolas are the pointers pointing to dynamic memory? Is it specified at all? – Aykhan Hagverdili Mar 09 '20 at 17:42
-
3[Temporary Object Lifetime](https://en.cppreference.com/w/cpp/language/lifetime#Temporary_object_lifetime) – Caleth Mar 09 '20 at 17:42
-
2@Ayxan: It's a temporary array. The array follows the rules of temporaries. – Nicol Bolas Mar 09 '20 at 17:42