literal and rvalue reference

房东的猫 提交于 2019-11-28 08:00:39

问题


void test(int && val)
{
    val=4;
}

void main()
{  
    test(1);
    std::cin.ignore();    
}

Is a int is created when test is called or by default in c++ literals are int type?


回答1:


Note that your code would compile only with C++11 compiler.

When you pass an integral literal, which is by default of int type, unless you write 1L, a temporary object of type int is created which is bound to the parameter of the function. It's like the first from the following initializations:

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



回答2:


An int with the value 1 is created when test is called. Literals are typed by their form. For example, 1 is an int, 1.0 is a double, "1" is a string.



来源:https://stackoverflow.com/questions/6864718/literal-and-rvalue-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!