How to embed nested initializer_lists in Visual C ++ 2013

I have a program that works in g ++ and clang using the initializer_list attached file. In Visual C ++, a 1D file works, but the 2D-attached file initializer_list does not work. Is there a trick for working with Visual C ++, or could it be a mistake in their implementation?

Here is my sample code. It works in Visual C ++ 2013 if I delete the annotated string.

#include <iostream> #include <initializer_list> using namespace std; template<class T> void print(T val) { cout << val; } template<class T> void print(initializer_list<T> lst) { bool first = true; cout << "["; for (auto i : lst) { if (!first) cout << ", "; print(i); first = false; } cout << "]"; } template<class T> void print(initializer_list<initializer_list<T>> lst) { bool first = true; cout << "["; for (auto i : lst) { if (!first) cout << ", "; print(i); first = false; } cout << "]"; } int main() { print({1, 2, 3}); cout << endl; // Without this line, Visual C++ 2013 is happy print({{1, 2}, {3, 4, 5}, {6}}); } 
+7
c ++ c ++ 11 visual-c ++ visual-studio-2013 initializer-list
source share
1 answer
 template<class T> std::initializer_list<T> list( std::initializer_list<T>&& l ) { return std::move(l); } 

or something like this can at least give you a workaround:

 print( { list({1,2}), list({3,2,1}) } ); 

the syntax can be confused with ( l*{2,1} ) in several ways.

+1
source share

All Articles