std::map, std::unordered_map - 缩小初始值设定项列表中的转换范围

std::map, std::unordered_map - narrowing conversion in initializer list

本文关键字:std map 列表 范围 转换 unordered 缩小      更新时间:2023-10-16

是错误还是标准允许?

#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;
}
}

在带有 clang10 和 gcc10 的 wandbox.org 上进行测试。std::setstd::unordered_set没有这样的问题.

std::mapstd::unordered_map的元素类型是std::pair。问题是std::pair有一个模板化的构造函数,

template< class U1, class U2 >
constexpr pair( U1&& x, U2&& y );

使用std::forward<U1>(x)初始化first,使用secondstd::forward<U2>(y).

例如,给定{33.3, 54}U1被推导为doubleU2被推导为int,注意这是一个完全匹配,并且不需要转换来构造std::pair,那么也不会发生缩小转换。

另一方面,对于std::set,给定std::set<int> s {33.3};,将使用std::set的构造函数std::initializer_list<int>,并且std::initializer_list<int>{33.3}初始化,发生窄转换。