在C++中,为什么在嵌套的unordered_map上调用函数需要移动构造函数?

Dav*_*vid 0 c++ move-semantics c++11

我有一个嵌套的无序映射,它在C++ 11中包含一个自定义对象.它看起来类似于:

std::unordered_map<std::string, std::unordered_map<std::string, CustomClass>>
Run Code Online (Sandbox Code Playgroud)

此自定义类没有移动构造函数,也不能具有隐式构造函数.尝试分配到此结构时出错:

storageStruct[string_1][string_2].function(parameter);
Run Code Online (Sandbox Code Playgroud)

由于它进入模板,实际的错误链解释真的很长,但这是我觉得最适用的那个:

候选构造函数(隐式移动构造函数)不可行:需要1个参数,但提供了0

我不打算移动这个对象,只是调用它的成员函数.我该如何避免这种不必要的举动?

for*_*818 5

std::unordered_map::operator[]如果您请求的密钥没有,则需要无中心地创建元素,即值必须是默认可构造的.移动构造函数只是一个候选者.提供默认构造函数或不使用operator[](您可以使用unordered_map::find查找元素和unordered_map::insert插入元素).