编译器允许缩小 map 和 unordered_map 的转换 - 这是编译器错误还是标准允许?
#include <iostream>
#include <map>
#include <unordered_map>
int main()
{
std::unordered_map<int,int> mm {{44,44}, {33.3, 54}, {222.2,222.2}};
for(auto& [f,s] :mm) {
std::cout<<f<<" - "<<s<<std::endl;
}
std::map<int,int> m {{44,44}, {33.3, 54}, {222.2,222.2}};
for(auto& [f,s] :m) {
std::cout<<f<<" - "<<s<<std::endl;
}
return 0;
}
检查https://wandbox.org/ - clang10 和 gcc10 - 对于 set 和 unordered_set - 编译器禁止缩小转换,而使用 map 和 unordered_map - 情况不同。
这是调用构造函数的初始化
这允许转换。