考虑以下代码:
int v = 12;
std::reference_wrapper<int> x(v);
std::reference_wrapper<const int> y(x); // (1) This works
std::reference_wrapper<const int> z = x; // (2) This fails
如果我理解正确,(1)有效,因为它只需要一次用户转换,(2)失败,因为它涉及转换序列中的两次用户转换:
std::reference_wrapper<int>::operator int& and
std::reference_wrapper<const int>::(const int&)
因此, std::reference_wrapper<int>
不能隐式转换为std::reference_wrapper<const int>
在我使用std::is_convertible
trait 的地方破坏我的一些代码。
这种设计是否有任何理由涉及不存在通用复制构造函数:
template <typename Y>
reference_wrapper<T>::reference_wrapper(const reference_wrapper<Y>&)
(就像在std::shared_ptr
例如)将允许这样的隐式转换?