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. list集合、txt文件对比的工具类和文件读写工具类

    工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...

  2. python之os和sys模块的区别

    一.os模块 os模块是Python标准库中提供的与操作系统交互的模块,提供了访问操作系统底层的接口,里面有很多操作系统的函数 1.os常用方法 import os # print(os.getcwd ...

  3. Different Integers

    牛客一 J题 树状数组 题目描述 Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, ...

  4. Mysql Sql 语句练习题 (50道)

    MySql 语句练习50题 表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_ ...

  5. LG_2967_[USACO09DEC]视频游戏的麻烦Video Game Troubles

    题目描述 Farmer John's cows love their video games! FJ noticed that after playing these games that his c ...

  6. QTP基本循环异常遍历(代码方式实现)

    0 环境 系统环境:win7 1 前言 在正常循环的基础下 添加异常处理遍历 一些基本操作 请看正常循环 https://www.cnblogs.com/my-ordinary/p/11739180. ...

  7. 63)PHP,登录验证

    首先辨析两种状态:   你的用户名和密码通过验证  只能表明你能登录,但是不能保证你登录了. 管理员信息合法和管理员处于的登录状态是两个概念:管理员信息合法证明你的用户名和密码是正确的, 但是管理员信 ...

  8. 吴裕雄--天生自然 R语言开发学习:R语言的简单介绍和使用

    假设我们正在研究生理发育问 题,并收集了10名婴儿在出生后一年内的月龄和体重数据(见表1-).我们感兴趣的是体重的分 布及体重和月龄的关系. 可以使用函数c()以向量的形式输入月龄和体重数据,此函 数 ...

  9. leetcode第32题:最长有效括号

    关于括号匹配或生成的问题,首先想到的是栈. 本题易错点:返回值为连续有效()的长度, 即()(()这种情况下,返回值为2 # 去除字符串首的连续)和字符串尾得连续( while True: if no ...

  10. Nginx笔记总结十七:nginx生成缩略图配置(http_image_filter_module)

    编译: ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_realip_module-- ...