std::map、std::unordered_map - 缩小初始化列表中的转换
std::map, std::unordered_map - narrowing conversion in initializer list
是bug还是标准允许的?
#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;
}
}
在 wandbox.org 上使用 clang10 和 gcc10 对其进行测试。 std::set
和 std::unordered_set
没有这样的问题。
std::map
和std::unordered_map
的元素类型是std::pair
。问题是 std::pair
有一个模板化的构造函数,
template< class U1, class U2 >
constexpr pair( U1&& x, U2&& y );
Initializes first
with std::forward<U1>(x)
and second
with
std::forward<U2>(y)
.
例如给定 {33.3, 54}
,U1
推导为 double
,U2
推导为 int
,请注意,这是一个精确匹配,此构造函数不需要转换用于构造 std::pair
,那么也不会发生缩小转换。
另一方面,对于 std::set
,给定 std::set<int> s {33.3};
,std::set
的构造函数将使用 std::initializer_list<int>
,并且 std::initializer_list<int>
是从 {33.3}
初始化,发生窄转换。
是bug还是标准允许的?
#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;
}
}
在 wandbox.org 上使用 clang10 和 gcc10 对其进行测试。 std::set
和 std::unordered_set
没有这样的问题。
std::map
和std::unordered_map
的元素类型是std::pair
。问题是 std::pair
有一个模板化的构造函数,
template< class U1, class U2 > constexpr pair( U1&& x, U2&& y );
Initializes
first
withstd::forward<U1>(x)
andsecond
withstd::forward<U2>(y)
.
例如给定 {33.3, 54}
,U1
推导为 double
,U2
推导为 int
,请注意,这是一个精确匹配,此构造函数不需要转换用于构造 std::pair
,那么也不会发生缩小转换。
另一方面,对于 std::set
,给定 std::set<int> s {33.3};
,std::set
的构造函数将使用 std::initializer_list<int>
,并且 std::initializer_list<int>
是从 {33.3}
初始化,发生窄转换。