#include <vector> #include <string> #include <iostream> struct PersonA { int age; std::string name; PersonA(int _age, const std::string& _name) : age(_age), name(_name) {} }; struct PersonB { int age; std::string name; PersonB(int _age, const std::string&& _name): age(_age), name(_name) {} }; struct PersonC { int age; std::string name; }; int main() { std::vector<PersonA> personA; personA.emplace_back(10, "nameA"); // fine std::vector<PersonB> personB; personB.emplace_back(10, "nameB"); // fine std::vector<PersonC> personC; //personC.emplace_back(10, "nameC"); // (the implicit move constructor) not viable // (the implicit default constructor) not viable personC.emplace_back(); // UPDATE: fine. }
Questions> Why vector::emplace_back requests an explicit constructor definition, otherwise the next line does not work?
// why it cannot make use of the default constructor of PersonC? personC.emplace_back(10, "nameC");
In addition, vector::emplace_back does not support uniform rendering. Does this relate to the above problem?
thanks
q0987 source share