字面值和右值引用 [英] literal and rvalue reference

查看:571
本文介绍了字面值和右值引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

int 当调用 test 或在c ++文字中默认为 int 类型时创建?

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

推荐答案

请注意,您的代码将使用C ++ 11编译器编译 only

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

当你传递一个默认为 int 类型的整数文字时,除非你写 1L ,创建类型为 int 临时对象,它绑定到函数的参数。它就像下面初始化中的第一

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

这篇关于字面值和右值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆