工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解。
这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正。
*************************Code*************************
class Dog
{
public:
Dog(){}
virtual ~Dog(){}
};
void NonConstReference (Dog & dog )
{
//tell the dog to do something here
}
void TestNonConstReference ()
{
NonConstReference( Dog());
}
*************************VS 2013, Level4 (/W4)*************************
warning C4239: nonstandard extension used : 'argument' : conversion from 'Dog' to 'Dog &'
*************************GCC, C++11*************************
-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -fexceptions -g -std=c++11 -c G:\MyBackup\code\CodeBlock\Test\main.cpp -o obj\Debug\main.o
G:\MyBackup\code\CodeBlock\Test\main.cpp: In function 'void TestNonConstReference()':
G:\MyBackup\code\CodeBlock\Test\main.cpp:18:29: error: invalid initialization of non-const reference of type 'Dog&' from an rvalue of type 'Dog'
G:\MyBackup\code\CodeBlock\Test\main.cpp:11:6: error: in passing argument 1 of 'void NonConstReference(Dog&)'
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))
*************************lvalue, xvalue, prvalue的一般定义*************************
首先lvalue, rvalue 都是针对表达式的;任何一个表达式都可以按照如下归类方式归类:
lvalue指代一个函数或者对象。例如:
- E是指针,则*E是lvalue
- 一个函数的返回值是左值引用,其返回值是lvalue。例如int& foo();
xvalue指代一个对象,但是和lvalue不同,这个对象即将消亡。
prvalue指代一个临时对象、一个临时对象的子对象或者一个没有分配给任何对象的值。例如:
- 一个函数的返回值是平常类型,其返回值是rvalue。例如int foo();
- 没有分配给任何对象的值。如5.3,true。
*************************lvalue, xvalue, prvalue的区分*************************
说明:这部分来自C++ PROGRAMMING LANGUAGE 4TH EDTION。
There are two properties that matter for an object when it comes to addressing, copying, and moving:
• Has identity: The program has the name of, pointer to, or reference to the object so that it is possible to determine if two objects are the same, whether the value of the object has changed, etc.
• Movable: The object may be moved from (i.e., we are allowed to move its value to another location and leave the object in a valid but unspecified state, rather than copying;).
It turns out that three of the four possible combinations of those two properties are needed to precisely describe the C++ language rules (we have no need for objects that do not have identity and
cannot be moved).
Using ‘‘m for movable’’ and ‘‘i for has identity,’’ we can represent this classification of expressions graphically:
.png)
So, a classical lvalue is something that has identity and cannot be moved (because we could examine it after a move), and
a classical rvalue is anything that we are allowed to move from.
*************************ISO IEC 14882 2011 8.5.3 References*************************
ISO文档使用cv来代表const volatile 修饰符。
并且假设我们使用这样的一种方式来赋值:cv1 T1 dest = cv2 T2 src;
举个例子就是:
int src = 123;
const int& dest = src;
void function(const int& dest){};
function(src);
ISO文档首先给出了两个概念:reference-related, reference-compatible。
Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related to “cv2 T2” if
- T1 is the same type as T2, or
- T1 is a base class of T2.
“cv1 T1” is reference-compatible with “cv2 T2” if
- T1 is reference-related to T2 and
- cv1 is the same cv-qualification as, or greater cv-qualification than, cv2.
说明:cv1 >= cv2的情况都有哪些呢:const > 没有修饰符, const volatile > const,etc.
分析一次赋值:cv1 T1 dest = cv2 T2 src; 是否合法采用如下4个步骤:
1.如果dest 是一个lvalue reference,同时:
1.1如果src是一个左值(不是一个bit-filed),并且cv1 T1 是 reference-compatible with cv2 T2的;
1.2如果T2是一个类类型(class, struct, union, etc.),即使cv1 T1 不是 reference-compatible with cv2 T2的,只要cv2 T2可以被转换成cv3 T3类型的一个左值(src1),这时如果cv1 T1 是 reference-compatible with cv3 T3的;
那么,dest 就帮定到src,或者src1上。
2.如果cv2 T2 src不能满足1.1,1.2,那么cv1 就应该是一个包含const的lvalue reference定义,否则它就因该是一个rvalue reference。此时如果cv2 T2满足如下条件:
2.1如果src是一个xvalue, 类类型的prvalue, array prvalue 或者返回左值的函数,并且cv1 T1 是 reference-compatible with cv2 T2的;
2.2如果cv2 T2是类类型的,即使cv1 T1 不是 reference-compatible with cv2 T2的,只要cv2 T2可以被转换成cv3 T3类型的一个2.1规定的值,假设是src1;
那么,dest就帮定到src,或者src1上。
3.如果cv2 T2 src也不能满足2.1,2.2,那么编译器就为src创建一个临时变量。
3.1创建此临时变量的条件是:cv1 T1 是 reference-related with cv2 T2,并且cv1 >= cv2;
4.如果cv2 T2 src不能满足上面所有的条件,那么cv1 T1就应该是一个rvalue reference。此时,如果cv2 T2是一个lvalue的话,编译器应该抱错。
*************************Reference 匹配(过滤)过程*************************
**************************************这里有些例子**************************************
-------------------------------能被规则1处理完毕-------------------------------------------------
double d = 2.0;
double& rd = d; //d, is an lvalue, and the cv1 equals cv2, 1.1能够处理
const double& rcd = d; // d, is an lvalue,
// the cv1 >= cv2: const > 没有修饰符,1.1能够处理
struct A { };
struct B : A
{
operator int&();
} b;
A& ra = b; // b, has a class type: struct;
//cv1 is reference related with cv2, ra is the base class of the b,1.2能够处理
const A& rca = b; // b, has a class type, struct;
//cv1 is reference related with cv2, ra is the base class of the b;
// the cv1 >= cv2: const > 没有修饰符,1.2能够处理
int& ir = B(); // B(), has a class type: struct
//it can be converted to an lvalue of the int&: operator int&()
//cv1 == cv2: cv修饰符都是空,1.2能够处理
------------------------------不符合规则1,被规则2处理-----------------------------------------
extern B f();
const A& rca2 = f();// f()返回值是一个类类型的rvalue,
// the cv1 >= cv2: const > 没有修饰符,2.1能够处理
struct X {
operator B();
operator int&();
} x;
const A& r = x;// x 是类类型的
// r 与x 不是reference-compatible的
// x 通过operator B()返回一个类类型的prvalue, tmpB
// r 与tmpB 的关系满足2.1的条件,2.2能够处理
-----------------------不符合规则1,也不符合规则2,被规则3处理---------------------------
const double& rcd2 = 2; // 2,不是一个lvalue/xvalue/类类型的prvalue/函数返回的左值,等。
// 创建一个临时变量2.0,3能够处理
--------不符合规则1,也不符合规则2,也不符合规则3,被规则4处理----------------------
double d2 = 1.0;
double&& rrd2 = d2; // rrd2是一个rvalue reference,不能使用lvalue 赋值。4能够处理
-----------------------------------------其他一些例子-------------------------------------------------------------
const volatile int cvi = 1;
const int& r2 = cvi; // error, in this example, the cv1 <= cv2, which violate the 1.1
*************************回到我们的例子*************************
class Dog
{
public:
Dog(){}
virtual ~Dog(){}
};
void NonConstReference (Dog & dog )
{
//tell the dog to do something here
}
void TestNonConstReference ()
{
NonConstReference( Dog());
}
NonConstReference( Dog())调用,在栈上创建了一个类类型的prvalue。
根据ISO文档,它不能规则1接纳,就只能由规则2继续处理。
规则2要求NonConstReference(Dog & dog )中的Dog & dog 必须是const Dog & dog。
而这里显然不是,所以抱错。
************************编译器为我们作了什么?语义分析*****************************
编译器,在严格的按照,c++语言的设计来执行语义检查:
- 目标是一个lvalue reference, 那么就不能给我一个rvalue.
- 要么就把目标设置成const lvalue reference.
如果一个参数是以非const引用传入,c++编译器就认为程序在函数中修改这个值,并且想要这个被修改过的值。
但如果你把一个临时变量当作非const引用参数传进来,程序并没有机会继续访问这样的变量,从而使修改一个临时变量变得毫无意义的。
从而c++编译器加入了临时变量不能作为非const引用的这个语义限制,意在限制这个非常规用法的潜在错误。
**************************************完*******************************************
- 重构手法之Introduce Explaining Variable(引用解释性变量)
返回总目录 6.5Introduce Explaining Variable(引用解释性变量) 概要 你有一个复杂的表达式. 将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表 ...
- 临时变量不能作为非const类型引用形参的实参
摘要: 非const 引用形参只能与完全同类型的非const对象关联. 具体含义为:(1)不能用const类型的对象传递给非const引用形参: ( ...
- 临时变量不能作为非const引用
转自:http://blog.csdn.net/u011068702/article/details/64443949 1.看代码 2.编译结果 3.分析和解决 就拿f(a + b)来说,a+b的值会 ...
- 非const引用不能指向临时变量
没找到具体原因,MSDN看到下面这句,VC是从2008才有这一限制的,感觉就是从语法上对临时变量增加了限定,因为一般说来修改一个临时变量是毫无意义的,通过增加限定,强调临时变量只读语义.虽然实际上修改 ...
- awk引用外部变量及调用系统命令方法
目标:想用awk与scp命令批量传送文件 前提:先搭好主机间的免密登陆环境(参考:http://www.cnblogs.com/tankaixiong/p/4172942.html) 实现脚本方法: ...
- [转] C++临时变量的生命周期
http://www.cnblogs.com/catch/p/3251937.html C++中的临时变量指的是那些由编译器根据需要在栈上产生的,没有名字的变量. 主要的用途主要有两类: 1) 函数的 ...
- c++ 临时变量
C++的临时变量 它们是被神所遗弃的孩子,没有人见过它们,更没有人知道它们的名字.它们命中注定徘徊于命运边缘高耸的悬崖和幽深的深渊之间,用自己短暂的生命抚平了生与死之间的缝隙.譬如朝露,却与阳光无缘. ...
- C++临时变量的生命周期
C++ 中的临时变量指的是那些由编译器根据需要在栈上产生的,没有名字的变量.主要的用途主要有两类: 1) 函数的返回值, 如: string proc() { return string(" ...
- Mysql 临时变量的 定义 和 赋值 Set 和 Into 赋值; Swith Mysql版本 Case When的用法
一:临时变量的定义和赋值 DECLARE spot SMALLINT; -- 分隔符的位置 DECLARE tempId VARCHAR(64); -- 循环 需要用到的临时的Cid DECLARE ...
随机推荐
- Bootstrap模态框(MVC)
BZ这篇博客主要是为大家介绍一下MVC如何弹出模态框.本文如果有什么不对的地方,希望大神们多多指教,也希望和我一样的小菜多多学习.BZ在这里谢过各位. 首先要在页面加上一个点击事件: @Html.Ac ...
- 统计Apache或Nginx访问日志里的独立IP访问数量的Shell
1.把IP数量直接输出显示: cat access_log_2011_06_26.log |awk '{print $1}'|uniq -c|wc -l 2.把IP数量输出到文本显示: cat acc ...
- texturepacker打包图片,场景切换时背景图有黑边
在使用TexturePacker打包图片之后,背景图在场景切换(有切换动画)时,明显能看到有黑边,在百度之后解决了. 知乎上边有网友贴出了两种解决方法,我抄过来如下: 第一种: 修改 ccConfig ...
- C# Redis Server分布式缓存编程 --网络转载
这篇文章我将介绍如果用最简洁的方式配置Redis Server, 以及如何使用C#和它交互编程 一. 背景介绍 Redis是最快的key-value分布式缓存之一 缺点: 没有本地数据缓冲, 目前还没 ...
- (转)网上总结的 NIPS 201 参会感受
1. http://www.machinedlearnings.com/2016/12/nips-2016-reflections.html 2. http://blog.arpitmohan.com ...
- $.map和$.extend来复制数组(OA差旅)
$.map:对数组的每个元素调用定义的回调函数并返回包含结果的数组. 对数组用指定的方法. //将原数组中每个元素加 4 转换为一个新数组. //jQuery 代码: $.map( [0,1,2], ...
- 嵌套循环中break、continue的用法
在循环语句中经常会用到break.continue语句,基本用法如下: 一.break语句为退出当前循环,在嵌套循环中结果如下: var num=0; for(var i=0;i<5;i++){ ...
- ETL利器Kettle实战应用解析系列二 【应用场景和实战DEMO下载】
本文主要阅读目录如下: 1.应用场景 2.DEMO实战 3.DEMO下载 1.应用场景 这里简单概括一下几种具体的应用场景,按网络环境划分主要包括: 表视图模式:这种情况我们经常遇到,就是在同一网络环 ...
- .NET批量删除代码前的行号
1 EmEditor Pro.EditPlus .visual studio ,把有行号的代码粘贴进去,按住键盘的Alt键,然后用鼠标拖出选择框列选行号,最后按Delete删除行号; 2 使用正则 ...
- correlation filters in object tracking
http://www.cnblogs.com/hanhuili/p/4266990.html Correlation Filter in Visual Tracking系列一:Visual Objec ...