C++ non-const lvalue reference cannot bind to a temporary
1. 问题代码
#include <iostream>
#include <vector>
//注意begin和end形参都声明为引用
bool find_int(std::vector<int>::iterator &begin, std::vector<int>::iterator &end, int v){
while(begin != end){
if(*begin == v)
return true;
begin++;
}
return false;
}
int main(){
std::vector<int> a(3, 100);
//a.begin()和a.end()的返回值作为实参传入find_int中
std::cout << find_int(a.begin(), a.end(), 100) << std::endl;
}
2. 编译错误
g++ -Wall -std=c++11 -o hello hello.cpp

3. 原因分析
non-const lvalue reference cannot bind to a temporary
根据编译错误提示可以知道,不能将形参begin、end绑定到a.begin()和a.end()的返回值,因为该返回值是一个临时量,临时量的生命周期可能在a.begin()和a.end()执行完后就结束了。因此编译器认为普通引用绑定一个临时量,在find_int函数中可能会修改这个临时量,然而此时临时量可能已经被销毁,从而导致一些未定义的行为,因此编译器不允许将普通引用绑定到一个临时量上。
4. 解决方案
- 将普通引用改为常量引用(PS:改为常量引用后不能修改绑定的对象,只能读不能写)
bool find_int(const std::vector<int>::iterator &begin, const std::vector<int>::iterator &end, int v)
- 修改函数的形参声明,将引用改成普通变量
bool find_int(std::vector<int>::iterator begin, std::vector<int>::iterator end, int v)
- 用变量存储a.begin()和a.end()的返回值
std::vector<int>::iterator begin = a.begin();
std::vector<int>::iterator end = a.end();
std::cout << find_int(begin, end, 100) << std::endl;
5. Tips
- 为什么方案一可行呢?通过常量引用绑定临时量,临时量就不会销毁了吗?
- C++标准:assigning a temporary object to the const reference extends the lifetime of this object to the lifetime of the const reference.
- 常量引用会延长临时量的生命周期
- 普通引用只能绑定和引用类型相同的左值(PS:函数的返回值不是左值,因为无法对其进行赋值)
- 常量引用可以绑定临时量、常量或者可转换为引用类型的变量
6. 参考资料
- 常量引用绑定临时量
- C++ primer 第五版-P55、P201
C++ non-const lvalue reference cannot bind to a temporary的更多相关文章
- error: cannot bind non-const lvalue reference of type
这种问题一般是因为引用了匿名变量.涉及左值和右值的区别.一般函数的参数如果是一个表达式,那将会产生一个第3方的匿名变量传入这个函数中,此时如果引用,没用什么实际意义. c++中临时变量不能作为非con ...
- C++之error: cannot bind non-const lvalue reference of type ‘myString&’ to an rvalue of type ‘myString’
先看代码(不想看代码可以直接看代码后的问题描述) //header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #inc ...
- C++ const用法,看这一篇就够了!
本文主要介绍const修饰符在C++中的主要用法,下面会从两个方面进行介绍:类定义中使用const.非类定义中使用const 1. 非类定义中使用const 非类定义中使用const是指:在除了类定义 ...
- C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)
General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...
- Value Categories
Value categories Three primary categories primary categories mixed special Each C++ expression (an o ...
- C++11中rvalue references的使用
Rvalue references are a feature of C++ that was added with the C++11 standard. The syntax of an rval ...
- 彻底理解c++的隐式类型转换
隐式类型转换可以说是我们的老朋友了,在代码里我们或多或少都会依赖c++的隐式类型转换. 然而不幸的是隐式类型转换也是c++的一大坑点,稍不注意很容易写出各种奇妙的bug. 因此我想借着本文来梳理一遍c ...
- C++11引用临时变量的终极解析
工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解. 这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正. **** ...
- Object lifetime
Object lifetime Temporary object lifetime Storage reuse Access outside of lifetime Every object has ...
随机推荐
- Python与用户相交互
今日所得 Python中注释的重要性 Python与用户相交互: 1.输入 2.输出 3.格式化输出 Python的基本数据类型:int,float,str,list,dict,bool 运算符 1. ...
- QLIKVIEW添加数据库连接
1.点击文件 2.点击编辑脚本 3.点击 工具-ODBC管理员 4.添加DSN 5.里面的常规操作不再赘述 6.完成
- Python||NameError: name 'reload' is not defined
多半是运行如下代码时报错: import sysreload(sys)sys.setdefaultencoding("utf-8")123这段代码是为了解决Python中中文输出出 ...
- struts2 标签s:select在table中单行显示
<table class="query_form_table"> <tr> <th>用户 ...
- 吴裕雄--天生自然python学习笔记:Python MongoDB
MongoDB 是目前最流行的 NoSQL 数据库之一,使用的数据类型 BSON(类似 JSON). PyMongo Python 要连接 MongoDB 需要 MongoDB 驱动,这里我们使用 P ...
- 洛谷-P3809-后缀排序(后缀数组)
看了求后缀数组的倍增法之后很快就理解了,但是自己写的倍增法用map排序还是超时了.然后看了两天别人写的模板,题目是通过了,但感觉代码还是半懂半背的.以后多熟悉熟悉吧: 后缀数组 #include &q ...
- JS一维数组、多维数组和对象的混合使用
转载地址:http://blog.csdn.net/wangyuchun_799/article/details/38460515 引言 这篇文章的主要目的是讲解JavaScript数组和对象的混合使 ...
- 无人工地,原来是靠AI这样运行的
随着全世界逐渐进入老龄化社会,适龄工作人口将急剧减少,必然导致用工成本增加,施工方降低人工成本.提升施工效率和质量的需求会越来越强烈,数字化施工技术应用前景广阔.在过去的十年中,无人机迎来了自己发展的 ...
- stm32 flash 存储
转载自: http://bbs.elecfans.com/jishu_388272_1_1.html 说到STM32的FLSAH,我们的第一反应是用来装程序的,实际上,STM32的片内FLASH不仅用 ...
- js-dom运动我有废话要说
今天整个图片上传剪切的功能 我终于不负众望不卑不亢 毫无意外的没写上 写点新得 留给N年后爱看不看的自己 咋整呢 百度之 demo一下,我就知道 屁不多放 切入正题 在js运动时候要注意布局 布局写不 ...