使用 int&& 参数通过 int&& 进行函数初始化

Function initialization with int& argument by int&&

本文关键字:int 函数 初始化 参数 使用      更新时间:2023-10-16

我有以下代码:

#include <type_traits>
struct TType
{
    int a = 0;
    bool operator() (int&)
    {
        return true;
    }
};
int main()
{
    static_assert(std::is_same<decltype(std::declval<TType>()(std::declval<int>())), bool>::value, "wtf?");
    return 0;
}

如果我尝试用g++-4.8.2编译它,然后我收到一个错误:

main.cpp:321:82: error: no match for call to ‘(JetPlane) (int)’
static_assert(std::is_same<decltype(std::declval<JetPlane>()(std::declval<int>())), bool>::value, "wtf?");
                                                                                ^
main.cpp:265:8: note: candidate is:
struct JetPlane
       ^
main.cpp:279:7: note: bool JetPlane::operator()(int&)
bool operator() (int&)
     ^
main.cpp:279:7: note:   no known conversion for argument 1 from ‘int’ to ‘int&’

我看不懂note: no known conversion for argument 1 from ‘int’ to ‘int&’行。所以问题是:为什么g++将std::declval<int>()的返回类型解释为int而不是int&&行,尽管std::声明声明看起来像:

template< class T >
typename std::add_rvalue_reference<T>::type declval();

我明白禁止将int&&绑定到int。但是为什么编译器不打印:note: no known conversion for argument 1 from ‘int’ to ‘int&’行。可能是我不理解的东西和编译器改变了std::declval<int>()的返回类型从int&& intstd::declval<TType>()(std::declval<int>()) ?

谢谢你的帮助!

问题是,您不能将xvalue绑定到非const左值引用。

让我们看一下表达式

std::declval<int>()

std::declval<int>的返回类型确实是int&&。因此,上面的表达式是类型为int的xvalue表达式。注意,在c++中表达式从来没有引用类型。

但是你的操作符

bool operator() (int&)

通过非const左值引用接受实参。如果您将参数类型更改为const int&,即

bool operator() (const int&)

一切正常

不能使用右值引用初始化左值引用。令人困惑的是,T&&在不同的上下文中意味着不同的东西:

  • T&&用于定义对象时,右值引用可以绑定到右值,但定义的对象实际上是一个左值,可以绑定到左值引用
  • 当使用T&&声明函数的返回类型时,意味着返回的右值引用在所有目的下都像临时引用一样,因此不能绑定到左值引用。在重载解析的所有目的中,T&&类型被视为临时的T,似乎这就是编译器报告的内容。