Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

literal and rvalue reference

Tags:

c++11

rvalue

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?

like image 458
Guillaume Paris Avatar asked Jul 28 '11 19:07

Guillaume Paris


1 Answers

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
like image 168
Nawaz Avatar answered Oct 15 '22 05:10

Nawaz