#include <iostream>
#include <initializer_list>
#include <vector>
auto const v = std::vector<std::initializer_list<int>>{ { 0, 1, 2 }, { 3, 4 } };
int main()
{
for (auto const& l : v)
for (auto const& i : l)
std::cout << i << " ";
}
This code outputs garbage, e.g.: 13386064 0 -1305220240 32764 0
(under several different compilers).
If I change v
to a std::vector<std::vector<int>>
, or move the definition inside main
, it prints 0 1 2 3 4
as expected.
Why?