void test(int && val)
{
val=4;
}
void main()
{
test(1);
std::cin.ignore();
}
Run Code Online (Sandbox Code Playgroud)
是int在test调用时创建的,或者在c ++文字中是默认int类型?
请注意,您的代码只能使用C++ 11编译器进行编译.
当您传递默认int类型的整数文字时,除非您编写,否则会创建1L一个类型的临时对象,该对象int绑定到函数的参数.这就像以下初始化中的第一个:
int && x = 1; //ok. valid in C++11 only.
int & y = 1; //error, both in C++03, and C++11
const int & z = 1; //ok, both in C++03, and C++11
Run Code Online (Sandbox Code Playgroud)