literal and rvalue reference

后端 未结 2 1991
半阙折子戏
半阙折子戏 2020-12-21 00:19
void test(int && val)
{
    val=4;
}

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

Is a int is created when

相关标签:
2条回答
  • 2020-12-21 00:59

    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
    
    0 讨论(0)
  • 2020-12-21 01:00

    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.

    0 讨论(0)
提交回复
热议问题