std :: ref隐式转换为引用混乱

问题描述 投票:0回答:1

我知道一个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
1个回答
1
投票

您要使用的operator<<重载具有以下形式(来自cppreference.com):

© www.soinside.com 2019 - 2024. All rights reserved.