m47h m47h - 24 days ago 6
C++ Question

Emplacement of a vector with initializer list

i have a

std::vector<std::vector<double>>
and would like to add some elements at the end of it so this was my trial:

std::vector<std::vector<double> > vec;
vec.emplace_back({0,0});


but this does not compile whereas the following will do:

std::vector<double> vector({0,0});


Why can't emplace_back construct the element at this position? Or what am i doing wrong?

Thanks for your help.

Answer

Template deduction cannot guess that your brace-enclosed initialization list should be a vector. You need to be explicit:

vec.emplace_back(std::vector<double>{0.,0.});