Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not convert from brace-enclosed initializer list to std::vector

Tags:

c++

gcc

c++11

I saw a lot of similar questions, but I don't think I saw quite the same one. It's pretty basic. Some code from my lecturer is failing to compile, and I distilled the problem to this test case:

void foo(vector<int> v) {
}

void fooUsage() {
    foo({0, 1, 2});
}

This fails with:

could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>

Note: It works on GCC 5.0.0 20141228 but fails on my GCC 4.7.1 (tdm-1).

Sorry if this is too basic but I don't know C++11 very well.

like image 218
Stefan Monov Avatar asked Jan 21 '15 19:01

Stefan Monov


2 Answers

This is a known bug that was fixed in gcc 4.8.

like image 58
Drew Dormann Avatar answered Oct 22 '22 14:10

Drew Dormann


It turns out I only needed to add -std=c++11 to the gcc command line.

Note: I was mistakenly thinking that this is on by default, since I was also getting some warnings like this:

extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]

See how it says "enabled by default"? That's what was misleading me.

like image 24
Stefan Monov Avatar answered Oct 22 '22 15:10

Stefan Monov