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. 解决方案

  1. 将普通引用改为常量引用(PS:改为常量引用后不能修改绑定的对象,只能读不能写)
bool find_int(const std::vector<int>::iterator &begin, const std::vector<int>::iterator &end, int v)
  1. 修改函数的形参声明,将引用改成普通变量
bool find_int(std::vector<int>::iterator begin, std::vector<int>::iterator end, int v)
  1. 用变量存储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

  1. 为什么方案一可行呢?通过常量引用绑定临时量,临时量就不会销毁了吗?

    1. C++标准:assigning a temporary object to the const reference extends the lifetime of this object to the lifetime of the const reference.
    2. 常量引用会延长临时量的生命周期
  2. 普通引用只能绑定和引用类型相同的左值(PS:函数的返回值不是左值,因为无法对其进行赋值)
  3. 常量引用可以绑定临时量、常量或者可转换为引用类型的变量

6. 参考资料

  1. 常量引用绑定临时量
  2. C++ primer 第五版-P55、P201

C++ non-const lvalue reference cannot bind to a temporary的更多相关文章

  1. error: cannot bind non-const lvalue reference of type

    这种问题一般是因为引用了匿名变量.涉及左值和右值的区别.一般函数的参数如果是一个表达式,那将会产生一个第3方的匿名变量传入这个函数中,此时如果引用,没用什么实际意义. c++中临时变量不能作为非con ...

  2. 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 ...

  3. C++ const用法,看这一篇就够了!

    本文主要介绍const修饰符在C++中的主要用法,下面会从两个方面进行介绍:类定义中使用const.非类定义中使用const 1. 非类定义中使用const 非类定义中使用const是指:在除了类定义 ...

  4. C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)

    General 每一个C++表达式(一个操作符和它的操作数,一个字面值,一个变量名等等)都代表着两个独立属性:类型+属性分类.在现代C++中 glvalue(泛左值) = lvalue (传统意义上的 ...

  5. Value Categories

    Value categories Three primary categories primary categories mixed special Each C++ expression (an o ...

  6. C++11中rvalue references的使用

    Rvalue references are a feature of C++ that was added with the C++11 standard. The syntax of an rval ...

  7. 彻底理解c++的隐式类型转换

    隐式类型转换可以说是我们的老朋友了,在代码里我们或多或少都会依赖c++的隐式类型转换. 然而不幸的是隐式类型转换也是c++的一大坑点,稍不注意很容易写出各种奇妙的bug. 因此我想借着本文来梳理一遍c ...

  8. C++11引用临时变量的终极解析

    工作中遇到一个引用临时变量的问题,经过两天的学习,私以为:不仅弄明白了这个问题,还有些自己的独到见解. 这里使用一个简单的例子来把自己的学习过程和理解献给大家,如果有什么问题请不吝指正.   **** ...

  9. Object lifetime

    Object lifetime Temporary object lifetime Storage reuse Access outside of lifetime Every object has ...

随机推荐

  1. smooth curve|population|sample

    Distribution Shapes 由直方图到 smooth curve   1.this distribution of heights is bell shaped(or mound shap ...

  2. vue实现动态绑定class--(三目运算符)根据span数字内容改变其样式

    一.根据span数字内容改变数字本身样式(两种样式) <template> //使用三目运算符,判断当span的val是否小于0给其不同的class名 <span class=&qu ...

  3. OfficeCommandbarDesigner20170202.rar

    OfficeCommandbarDesigner用于对Office各个常用组件.VBE的工具栏进行查看和编辑的工具. 界面主要分为上下两个表格控件,上面的控件列举出所有Commandbar,下面的控件 ...

  4. mysql操作命令梳理-grant授权和revoke回收权限

    在mysql维护工作中,做好权限管理是一个很重要的环节.下面对mysql权限操作进行梳理: mysql的权限命令是grant,权限撤销的命令时revoke:grant授权格式:grant 权限列表 o ...

  5. 倾斜摄影数据转cesium 3D tiles工具介绍

    软件操作:http://jingyan.baidu.com/article/3aed632e3912c8701080912c.html 软件测试数据: 数据,油库链接: http://pan.baid ...

  6. Docker部署Python爬虫项目

    1) 首先安装docker: # 用 yum 安装并启动 yum install docker -y && systemctl start docker 2) 下载自定义镜像需要用到的 ...

  7. 吴裕雄--天生自然python学习笔记:pandas模块强大的数据处理套件

    用 Python 进行数据分析处理,其中最炫酷的就属 Pa ndas 套件了 . 比如,如果我 们通过 Requests 及 Beautifulsoup 来抓取网页中的表格数据 , 需要进行较复 杂的 ...

  8. 牛客-DongDong数颜色 及其相似题

    大佬博客 ps:在牛客上做到这题不会,学会之后补了两道相关题.顺便记录一下. 牛客-DongDong数颜色 sol:dfs序+莫队,先把树上的点标上dfs序,因为子树的dfs序是连续的,所以子树可以表 ...

  9. C# Dictionary字典类介绍

    说明    必须包含名空间System.Collection.Generic     Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)     键必须是唯一的,而值不需要唯 ...

  10. mybatis-generator二次开发总结

    二次开发(此文只作记录,具体代码及文章在内网,copy不出来) 自定义生成代码需求: 1.去除默认生成的example接口方法: (1)配置generatorConfig.xml (2)修改源码tab ...