Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 933 Bytes

File metadata and controls

51 lines (35 loc) · 933 Bytes

Container initialisation

C++11 introduces an uniform syntax for initialising containers with contained values.

Works with all types of containers, for example with std::vector, std::map, std::set, std::list and std::string. It uses the same syntax we are used for C arrays and structs.

Container initialisation

Before C++11

void foo(std::vector<int>);

std::vector<int> vi;
vi.push_back(42);
vi.push_back(-1);

foo(vi);

Since C++11

void foo(std::vector<int>);

std::vector<int> vi = {42, -1};

foo(vi);


//////////

foo({42, -1});

Container initialisation

One limitation (still present in C++20), is that the type requires a copy-constructor.

There is work going on to remove this limitation.