In my environment, the std::initializer_list
is implemented as a pointer to the first element, and a size. Still in my specific setup, I was able to observe that:
- the underlying data is allocated in the current function frame (because the pointer to the first element says so)
- returning an
initializer_list
by value from a function does not change the value of the pointer (leading to the conclusion that the data is not copied alongside the initializer_list).
This is making it unsafe to copy an initializer_list
, if the copy can outlive the original object.
- Will this behavior be maintained by further releases of the C++ standard ?
- And equally important, what would be the rationale behind this behaviour ? (it bit me really hard today, so I would naïvely say it goes against the beloved principle of "least astonishment")