【发布时间】:2020-07-25 20:10:31
【问题描述】:
我知道一个 std::ref(object) 创建一个 std::reference_wrapper(object),并且 std::reference_wrapper 有一个非显式类型转换运算符成员函数
operator T&() const
我知道当模板参数推导发挥作用时,这会影响我使用它的方式: 所以在下面的打印中,如果我用 std::ref("hello") 调用它,参数的类型 T 被推断为 std::reference_wrapper
template <class T>
void Print(T t)
{
std::cout << t << std::end;
}
为什么这行不编译?
std::string s = "hello";
std::cout << std::reference_wrapper<std::string>(s) << std::endl;
实例化的特化应该有一个
operator std::string&() const
类型转换函数,为什么我不能像这样使用引用包装器呢?
【问题讨论】:
标签: c++ type-conversion implicit-conversion ref reference-wrapper