使用 int&& 参数通过 int&& 进行函数初始化
Function initialization with int& argument by int&&
我有以下代码:
#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&&
int
在std::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
,似乎这就是编译器报告的内容。
相关文章:
- 调用'begin(int [n])'没有匹配函数
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- swap(int&, int&) 函数不起作用,当交换不使用临时变量时?
- 如何在不返回 C++ 值的情况下退出 int 函数?
- 调用 Parse int 函数时编译 parse 整数程序时出现问题
- 如何将字符串返回到主INT函数
- 为什么要使用int函数而不是void
- int 函数C++可以返回类似 "NULL" 的东西?
- C++int函数.当零是有效的返回值时,我如何检测false
- int const函数(参数)、int函数(const参数)和int函数(parameters)const之间的区别是什
- 类无法从主C++访问 int 函数
- 无法从 int 函数返回结果
- 如何将'int'函数调用到'void'函数
- 强制字符串为int函数以消耗整个字符串
- Int函数参数不能正常工作
- 删除Mat int函数(opencv)
- 为什么float参数适合int函数参数