Initializing vector with {}, expected a ";". List Initialization C++11.

Feel like I'm missing something painfully glaring, but I copied the code right off the book.

#include <iostream>
#include <vector>
using std::cin;
using std::cout; using std::endl;
using std::vector;

int main()
	vector<int> v{1,2,3,4,5,6,7,8,9};

	for (auto &i : v)
		i *= i;

	for (auto i : v)
		cout << i << " ";
	cout << endl;


//Error appears right after v.
vector<int> v{1,2,3,4,5,6,7,8,9};

What am I missing? =/

Last edited on
- I'm not sure that you can even initialize vectors as such.

- However, if you can, you would probably need an "=" sign between "v" and the first "{" on line 9.
Adding the "=" in between does state that initialization with '{...}' isn't allowed for object of type "std::vector<int, std::allocator<int>>".

Looking back in the book (C++ Primer 5th ed.), I think this kind of initialization is introduce in C++11. They call it "List Initializing".

I'm using VS Express 2012, isn't C++11 in that?
- If you cant initialize vectors, then use the push_back function of the vector template.
Thanks for the advice. The problem is with the compiler. Updating VS to include additional C++11 features (one of which is List Initialization) didn't seem to work for me. Link to the update.

There are other compiler alternatives but I guess this isn't a big deal right now. Just throwing this out there in case someone else is in the same wondering position as I am.
I don't use that particular compiler. However, it may be necessary to specify certain compiler options in order to enable / disable specific features.
I don't think this is supported in MSVS yet. VS2012 only has partial C++11 support.
If I understand this list correctly:

Initializer lists was added in 11.0 nov'12. Even the update page explicitly mentions this feature. I changed my project's Platform Toolset to the nov'12 update (following the instructions on the download page), but still doesn't seem to work.

Then again I'm still a scrub on all of this, so I'm not sure what other options are there to enable. Yeah Disch noticed that from the Apache list.

Code::blocks works though. I suppose GCC and Clang are the better compilers? (Pardon me if that isn't their right terminology)
Initializer lists were included in the Nov 12 CTP, however the library code wasn't modified to use them. I don't expect the std library will be until it's added as a non-preview type of feature.
Topic archived. No new replies allowed.