tomocafe
tomocafe

Reputation: 1544

Syntax errors when passing initializer list as function argument for vector in VS12

I have a function that takes in a vector as an input argument:

void expandVector(std::vector<int> inputVector, std::vector<int>& outputVector);

When I call this function, I typically only have 1 element in the input vector, so I pass in an initializer list with 1 element.

std::vector<int> expandedVector;
expandVector({1337}, expandedVector);

This works fine with gcc 4.8.2 but I get these errors when I try to compile with Visual Studio 2012:

source.cpp(353) : error C2143: syntax error : missing ')' before '{'
source.cpp(353) : error C2660: 'expandVector' : function does not take 0 arguments
source.cpp(353) : error C2143: syntax error : missing ';' before '{'
source.cpp(353) : error C2143: syntax error : missing ';' before '}'
source.cpp(353) : error C2059: syntax error : ')'

When I check the MSDN documentation for vector::vector, it lists the constructor with initializer list, and furthermore, the example shows it being used.

vector<int> v8{ { 1, 2, 3, 4 } };

Even though I don't explicitly declare and name a vector (like v8 in the example), shouldn't I still be able to pass in an initializer list to a function expecting a vector?

Upvotes: 1

Views: 490

Answers (1)

tomocafe
tomocafe

Reputation: 1544

Answering my own question, thanks to Columbo for pointing me in the right direction.

Visual Studio 2012 doesn't support initializer lists, see Support for C++11 Features (Modern C++) on MSDN.

Also, the page for vector::vector I originally referred to is for VS 2013. When I picked the correct VS 2012 version, I found that this constructor is not available.

The solution will be to replace

expandVector({1337}, expandedVector);

with

expandVector(std::vector<int>(1, 1337), expandedVector);

Upvotes: 1

Related Questions