#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.
}
问题> 为什么vector::emplace_back
请求显式定义构造函数,否则以下行不起作用?
// why it cannot make use of the default constructor of PersonC?
personC.emplace_back(10, "nameC");
此外,vector::emplace_back
不支持统一初始化。这是否与上述问题有关?
谢谢