1
std::vector<int> test { 0x34 };

gives this error:

error expected a ';'

I am using VS2012 which supports this C++11 feature.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451

1 Answers1

2

Your code is perfectly legal in C++11. I must conclude you are not using November 2012's CTP, in which case your assumption:

I am using VS2012 which supports this C++11 feature.

is incorrect. This Q&A on StackOverflow clarifies which features are supported by VC11.

Notice, that uniform initialization is only supported in November 2012's CTP.

And while it is true that November 2012's CTP does introduce support for C++11's uniform initialization in the compiler (see this Q&A on StackOverflow for an overview of what features are supported), the implementation of the standard library which is shipped with VS2012 has not been rewritten to make use of those features (Error when initializing a vector).

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • According to http://blogs.msdn.com/b/vcblog/archive/2012/11/02/visual-c-c-11-and-the-future-of-c.aspx Nov12 CTP supported initializer lists. – user2465415 Jun 08 '13 at 14:55
  • 1
    @user2465415: Quote from the "important notes" section: _This package contains only the compiler, and does not yet come with an updated standard library to use the features (**such as a std::vector initializer_list constructor**)._ – Mat Jun 08 '13 at 14:56
  • @Mat: Oh, OK, that explains. I'll have to edit the answer, thank you – Andy Prowl Jun 08 '13 at 14:57
  • Well, that's disappointing. Can anyone estimate how long that would take? – user2465415 Jun 08 '13 at 14:58
  • @user2465415: Indeed it seems MS is very much behind its competitors in this respect. I haven't read of any clear promises to catch up soon, although VS2013 should contain more C++11 features – Andy Prowl Jun 08 '13 at 15:00
  • @Mat: Actually, I'm a bit confused: even if `vector` does not have an `initializer_list` constructor, as long as support for uniform initialization is included in the compiler, the OP's initialization should invoke the constructor taking a size, shouldn't it? – Andy Prowl Jun 08 '13 at 15:03
  • Excuse the noob question but if I switched to the Intel Compiler or any other compiler, would this compile? Or would I still need to wait on MS to update their standard libraries? – user2465415 Jun 08 '13 at 15:03
  • @user2465415: If you only change the compiler, and not the implementation of the library, it still won't do what you want (it will initialize a vector of size `0x34`). Actually, that's what I would expect to happen in your case too. I'm puzzled. Are you sure you are using the CTP? – Andy Prowl Jun 08 '13 at 15:05
  • @AndyProwl: Oh, I didn't realize that you had to explicitly use CTP. I thought it's included in the visual studio updates (especially since it's an old update at this point). Although switching to CTP won't help me since I still would need the right implementation, right? – user2465415 Jun 08 '13 at 15:08
  • @user2465415: Exactly, installing the CTP will help you make your code compile, but it would still not do what you want – Andy Prowl Jun 08 '13 at 15:11
  • @AndyProwl: not exactly sure. Note that the size constructor takes a `size_t`, not an int, and is marked explicit. A quick test with clang++ with a dummy class shows clang++ selects the initializer list constructor in that case. But I'm no expert about this at all. – Mat Jun 08 '13 at 15:11
  • @Mat: Hm, the fact that the constructor is `explicit` only makes it ineligible for contexts other than direct-initialization, and here we do have a direct-initialization - so I still think it should be picked even though a conversion is required for the argument – Andy Prowl Jun 08 '13 at 15:17
  • @AndyProwl: Hm. Thinking about it a bit more, I don't see why a syntax error would be raised if the compiler does support that feature. The syntax is fine regardless of what constructors the template has... – Mat Jun 08 '13 at 15:24
  • @Mat: I don't think so. If the compiler does not support uniform initialization, then a syntax error will be issued. In C++03 we could not initialize objects with braces unless they were aggregates (and even in that case I think the `=` was necessary, although I'm not sure) – Andy Prowl Jun 08 '13 at 15:26
  • @AndyProwl: yes, that's what I was saying. There shouldn't be a syntax error if uniform initialization is supported. – Mat Jun 08 '13 at 15:27
  • @Mat: Yes, indeed. That's what I was mentioning a few comments above. But then the OP clarified he did not install the CTP, which is what I initially thought – Andy Prowl Jun 08 '13 at 15:29
  • Initializer list is partially supported in VC++, but it is not yet implemented in library codes (STL). That results in some codes to compile successfully but it does not mean Uniform Initialization being fully supported. – saki7 Jun 08 '13 at 15:55
  • How did this person get it to compile? http://stackoverflow.com/a/8906577/2465415 – user2465415 Jun 08 '13 at 16:40
  • But aren't they still using the STL library? I'm confused. :( I'm trying to find another implementation but I'm having trouble. – user2465415 Jun 08 '13 at 16:43
  • @user2465415: Different compilers usually come with different implementations of the Standard Library (btw, it's Standard Library, not STL - the STL is a now obsolete library that inspired *part* of the C++ Standard Library). MSVC can only work with MS implementation of the Standard Library. – Andy Prowl Jun 08 '13 at 16:45
  • @user2465415: You can use GCC with libstdc++, for instance. If you're working on Windows, check out the MinGW distribution – Andy Prowl Jun 08 '13 at 16:46
  • Thanks Andy. Appreciate it. – user2465415 Jun 08 '13 at 16:48