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.

Share:
23,732

Related videos on Youtube

Polaris878
Author by

Polaris878

Updated on November 11, 2020

Comments

  • Polaris878
    Polaris878 over 3 years

    Possible Duplicate:
    C++11 features in Visual Studio 2012

    So 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
      Polaris878 over 11 years
      Yet 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
      Marlon over 11 years
      I agree with Polaris. MSDN angers me.
    • Bimo
      Bimo almost 6 years
      make sure you don't forget the commas... vector<string> x = {"one" "two"}; its a concatenation of two strings..
  • Polaris878
    Polaris878 over 11 years
    Thanks 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
    dascandy almost 11 years
    The 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
    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
    Claudiu about 9 years
    Hmm it's 2015 and VS 2012 still doesn't seem to support it..