Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
613 views
in Technique[技术] by (71.8m points)

c++11 - literal and rvalue reference

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...