【发布时间】:2021-06-25 23:00:05
【问题描述】:
除非我取消注释默认构造函数声明,否则此示例无法编译:
#include<unordered_map>
#include <iostream>
struct foo{
int data;
/*foo(){
data = 0;
std::cout << "DEFAULT\n";
}*/
foo(int d){
data = d;
std::cout << "PARAM\n";
}
};
struct bar{
std::unordered_map<int, foo> map;
foo getElem(int i){
return map[i];
}
};
int main() {
bar b;
foo f1(1);
foo f2(2);
b.map.insert({1,f1});
b.map.insert({2,f2});
foo f3 = b.getElem(1);
}
/opt/compiler-explorer/gcc-10.2.0/include/c++/10.2.0/tuple:1689:70: error: no matching function for call to 'foo::foo()'
1689 | second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
| ^
当我取消注释默认构造函数声明并成功编译时,面包屑显示从未调用过默认构造函数。
这里发生了什么?类似的问题是由于most vexing parse,但没有任何显式的构造函数调用,我不确定这里是否是这种情况。
This answer 提供了关于为什么 unordered_map 会隐式调用默认构造函数的提示。问题是所描述的记录行为和 MVP 的某种组合吗?
【问题讨论】:
-
the breadcrumbs show that the default constructor is not ever called.它可能是,如果return map[i];访问一个不存在的元素。
标签: c++ unordered-map default-constructor most-vexing-parse