笔记--c++临时对象与const

原创 2007年09月20日 14:24:00
c++中规定临时对象只能赋给const的引用
const A& a = A()  //合法
A& a = A()            //非法
百思不得其解,不知道为什么c++会这样规定,发帖问得到如下答案:
It is likely to lead to unexpected behaviour. Imagine something first like this:
Code:

std::string & make_upper( std::string & s )
{
std::transform( s.begin(), s.end(), s.begin(), ::toupper );
return s;
}

Reasonable enough implementation. Now first suppose you tried to pass in char * instead.

Code:

char text[] = "Hello world";
make_upper( char_text );


std::string has an implicit constructor from const char * (and thus char *) so it might create a temporary which can be used for this function, but if that were allowed, the std::string makes a copy so you would only be modifying the copy and the changes would be lost.

In case you think that this should apply only to implicit constructors and not temporaries returned from functions, there are a lot of cases whereby objects are constructed through a helper function because these functions can resolve the template parameters (needed for the class) so you don't need to supply them to the function. (bind1st and make_pair are examples of such functions).

Note that if the class is a wrapper for a non-const reference or pointer, you can work around the situation by using a const reference to the buffer. For example in class to input to a vector.

Code:

template< T >
class vec_wrapper_t
{
std::vector< T > & m_v;
public:
vec_wrapper_t( std::vector< T > & v ) : m_v ( v )
{
}

void input( std::istream & is ) const
{
// implement input to the vector
}
};

template < typename T >
vec_wrapper_t< T > vec_wrapper( std::vector< T > & v )
{
return vec_wrapper_t< T >( v );
}

template < typename T >
std::istream & operator>>( std::istream & is, const vec_wrapper_t & vw )
{
vw.input( is );
return is;
}

Above illustrated
1. That you pass in vec_wrapper_t as const reference (or value) even to the overload of operator >> from istream
2. vec_wrapper is a typical function that resolves the template parameter for you and returns a temporary.
3. with the code above, if v is a vector and is is an input stream you can write is >> vec_wrapper( v ) in your code because of the fact that it's a const reference.
4. In reality vec_wrapper_t is going to have other features, (in my case it has the delimiters used for inputting). You can't get operator>> to write these because you vec_wrapper_t is temporary, i.e. anything you wrote to it would be lost and for the next vector you tried to write to, it would not be there.

reply by me:
what do you mean is in c++ there exists plenty of codes which may create temporary objects, if we do not define temporary as const, we will suffer much more buggy codes? With the definition of temporary as const, the compiler can find out these bugs ?

函数返回的临时对象为const类型,const引用

#include #include     using namespace std; class A { public: A(){ m_Int = 10; } publi...
  • damotiansheng
  • damotiansheng
  • 2014年02月17日 15:57
  • 2650

C++中临时对象的产生

//《More Effective C++》读书笔记 C++真正的所谓临时对象使不可见的----不会在你的源代码中出现。 (不要把代码中出现的局部对象误解为临时对象) 临时对象通常发生于两种情...
  • sichuanpb
  • sichuanpb
  • 2017年04月13日 10:01
  • 119

C++中指针,引用与const,以及const中的临时量对象的探讨

刚学完C++中的指针,引用和const,以及三者之间的qu'b
  • TouerCheng
  • TouerCheng
  • 2014年07月24日 17:07
  • 640

C++中临时对象的产生与优化

C++中临时对象的产生与优化 本文主要介绍c++中临时对象产生的几种情况,同时介绍避免的策略。由于在C++中对象的创建和消除会调用该对象对应的构造和析构函数,是一个相对比较耗时的操作,从程序效率角度来...
  • fangqingan_java
  • fangqingan_java
  • 2013年07月13日 20:55
  • 1180

C++临时对象那些事儿

C++大概是这个世界上最飘逸、成功、失败的语言吧,临时对象是C++语言中最复杂的东西之一。 以下代码段新手大概经常会写吧: std::string FetchFormat(){ return "%d...
  • wind_2008_06_29
  • wind_2008_06_29
  • 2016年04月13日 16:55
  • 1366

C++ const引用、临时变量 引用参数

C++引用—临时变量、引用参数和const引用 如果实参与引用参数不匹配,C++将生成临时变量。如果引用参数是const,则编译器在下面两种情况下生成临时变量:          实参类型是正确的...
  • yusiguyuan
  • yusiguyuan
  • 2015年02月05日 11:48
  • 3087

c++ const 成员函数 & 临时变量 & 右值引用 & move

const 成员函数尊重原作,部分转自http://blog.csdn.net/lihao21/article/details/8634876我们知道,在C++中,若一个变量声明为const类型,则试...
  • GW569453350game
  • GW569453350game
  • 2015年07月27日 09:41
  • 3521

C++中函数返回临时对象和本地对象的区别

C++中函数返回临时对象和本地对象的区别                        By qianghaohao(CodeNutter)        在C++中如果函数返回值是对象的时候,那么该...
  • qianghaohao
  • qianghaohao
  • 2016年09月09日 20:48
  • 1529

C++调用函数是如何返回临时对象的

请看如下代码 #include "stdafx.h" #include using namespace std; class INT { friend ostream& operator...
  • u012332679
  • u012332679
  • 2017年03月07日 18:03
  • 375

C++临时对象销毁时间

下面这段代码会输出什么?
  • passion_wu128
  • passion_wu128
  • 2014年08月31日 12:29
  • 1782
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:笔记--c++临时对象与const
举报原因:
原因补充:

(最多只允许输入30个字)