Initializer list not working with vector in Visual Studio 2012?
23,732
Visual Studio 2012 does not support initializer lists.
Well, it didn't until the November 2012 CTP. Now it does, at least in an alpha state. Granted, this code still won't work in it because they're still putting initializer lists into the standard library itself.
Related videos on Youtube
Author by
Polaris878
Updated on November 11, 2020Comments
-
Polaris878 over 3 years
Possible Duplicate:
C++11 features in Visual Studio 2012So I was reading up on C++11 initializer lists today via Wikipedia and saw that C++11 supports the following syntax for the standard containers:
std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" }; std::vector<std::string> v({ "xyzzy", "plugh", "abracadabra" }); std::vector<std::string> v{ "xyzzy", "plugh", "abracadabra" };
When I try the following in Visual Studio 2012 I get the compilation error
C2552: 'vecs' : non-aggregates cannot be initialized with initializer list
Here is my code:
#include <vector> using namespace std; int main() { vector<string> vecs = {"h", "g", "e"}; }
Does VS2012 not support initializer lists or am I just misunderstanding something?
Thanks!
-
Polaris878 over 11 yearsYet it was much quicker for me to ask a question on SO than read some poorly laid out MSDN article. I guess I shouldn't post to SO next time.
-
Marlon over 11 yearsI agree with Polaris. MSDN angers me.
-
Bimo almost 6 yearsmake sure you don't forget the commas... vector<string> x = {"one" "two"}; its a concatenation of two strings..
-
-
Polaris878 over 11 yearsThanks Nicol, here is a fairly good table describing the list of features VS2012 supports: blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
-
dascandy almost 11 yearsThe support is atrocious though.
int main() { for (int i : {1, 2, 3, 4, 5}) { } }
gives an internal compiler error. edit actually, the pre-CTP one crashes. The CTP one just lacks the begin / end functions for initializer lists. -
Nicol Bolas almost 11 years@dascandy: Yes. That's part of the lack of standard library support for initializer lists: not having proper overloads for std::begin and std::end, which ranged-based
for
relies on. -
Claudiu about 9 yearsHmm it's 2015 and VS 2012 still doesn't seem to support it..