为什么 std::pair 对于 const 引用和转发引用参数有两个不同的构造函数?
Why does std::pair have two different constructors for both const reference and forwarding reference parameter?
来自 ISO 标准(准确地说是 N4860)std::pair
简介:
constexpr explicit(see below) pair(const T1& x, const T2& y); // first constructor
template<class U1, class U2>
constexpr explicit(see below) pair(U1&& x, U2&& y); // second constructor
我似乎找不到任何理由为什么第一个构造函数应该与完美转发构造函数一起定义。完美的转发构造函数是否足以处理复制、移动情况?在哪种情况下第一个构造函数在重载决议中获胜?
In which case does first constructor win in overload resolution?
当传递与 std::pair
的成员具有完全相同类型的 const 左值时,它获胜,即 const T1
和 const T2
。两个构造函数都是完全匹配的,non-template 一个会赢。例如
const int i = 0;
const int j = 0;
std::pair<int, int> p(i, j);
自 C++11 以来添加了采用转发引用的构造函数,我认为第一个保留只是为了保持一致性。
来自 ISO 标准(准确地说是 N4860)std::pair
简介:
constexpr explicit(see below) pair(const T1& x, const T2& y); // first constructor
template<class U1, class U2>
constexpr explicit(see below) pair(U1&& x, U2&& y); // second constructor
我似乎找不到任何理由为什么第一个构造函数应该与完美转发构造函数一起定义。完美的转发构造函数是否足以处理复制、移动情况?在哪种情况下第一个构造函数在重载决议中获胜?
In which case does first constructor win in overload resolution?
当传递与 std::pair
的成员具有完全相同类型的 const 左值时,它获胜,即 const T1
和 const T2
。两个构造函数都是完全匹配的,non-template 一个会赢。例如
const int i = 0;
const int j = 0;
std::pair<int, int> p(i, j);
自 C++11 以来添加了采用转发引用的构造函数,我认为第一个保留只是为了保持一致性。