1

I am compiling this simple code as g++ main.cpp -o main -std=c++03

#include <vector>
int main(){
    std::vector<int> array;
    std::vector<int> array2 = { 9, 7, 5, 3, 1 };
}

And Im getting the following compilation error:

main.cpp: In function ‘int main()’:
main.cpp:39:18: error: in C++98 ‘array2’ must be initialized by constructor, not by ‘{...}’
std::vector array2 = { 9, 7, 5, 3, 1 };
                  ^~~~~~
main.cpp:39:43: error: could not convert ‘{9, 7, 5, 3, 1}’ from ‘’ to ‘std::vector’
std::vector array2 = { 9, 7, 5, 3, 1 };

It seems that even though that I am compiling with the -std=c++03 (where the initialization list are available), I am still using C++98 standard. Why is this happening?

I know that this code will compile with newer standards.

Burak
  • 2,251
  • 1
  • 16
  • 33
learning_dude
  • 1,030
  • 1
  • 5
  • 10
  • 2
    "(where the initialization list are available)" your premise is wrong. Where do you get that from? C++03 is not much different from C++98, actually from the top of my head I cannot tell any difference between the two – 463035818_is_not_an_ai Mar 28 '20 at 15:56
  • from here..https://www.learncpp.com/cpp-tutorial/6-16-an-introduction-to-stdvector/comment-page-1/#comments – learning_dude Mar 28 '20 at 16:00
  • 1
    the answer there also says that you need c++11 – 463035818_is_not_an_ai Mar 28 '20 at 16:02
  • the confusion comes then from the first example on that webpage. Makes me think it is cpp++03 compliant . – learning_dude Mar 28 '20 at 16:09
  • 1
    "Introduced in C++03, std::vector ..." this is wrong/misleading. I dont know to what exactly this is supposed to refer. `std::vector` was already there in C++98. Don't worry, this online tutorial containing wrong/misleading information is not an exception but rather the rule. I advise you to learn from a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead – 463035818_is_not_an_ai Mar 28 '20 at 16:23
  • 1
    the only way I can explain that to myself is that they use c++03 and c++98 as synonyms, which is justified to some extend, as c++03 was mainly bugfixes and only introduced a single new feature (I had to look it up, it was value initialization) – 463035818_is_not_an_ai Mar 28 '20 at 16:33

1 Answers1

5

Why is this happening?

Because the syntax is only available for aggregate initialisation. std::vector is not an aggregate, thus it cannot be aggregate initialised.

C++11 introduced more general list initialisation, as well as std::initializer_list type, and a constructor to vector and other containers that will be used with list initialisation syntax.


The article that you linked in the comments is not perfect:

Introduced in C++03, std::vector provides ...

std::vector was introduced in the very first standard version C++98.

std::vector<int> array2 = { 9, 7, 5, 3, 1 }; // use initializer list to initialize array
std::vector<int> array3 { 9, 7, 5, 3, 1 }; // use uniform initialization to initialize array (C++11 onward)

It is highly confusing to not mention that the first line also requires C++11.

eerorika
  • 232,697
  • 12
  • 197
  • 326