它是更有效的返回一个const引用 [英] Is it more efficient to return a const reference

查看:90
本文介绍了它是更有效的返回一个const引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如



这些之中最好的是:

  std :: string f(){} 

  const std :: string& f(){} 


解决方案

从不返回对本地对象/变量的引用,因为此类对象超出范围并在函数返回时被销毁。



不同的是,函数可以为范围不受函数上下文限制的对象返回一个const或非const引用。典型的例子是自定义运算符<<

  std :: ostream& operator<<<(std :: ostream& out,const object& obj)
{
out< obj.data();
return out;
}

不幸的是,return-by-value具有性能缺点。正如Chris所说,按值返回一个对象涉及一个临时对象的副本及其后续的销毁。复制发生我的手段复制构造函数或operator =。为了避免这些低效率,智能编译器可以应用RVO或NRVO优化,但是在有些情况下它们不能多次返回。



即将到来的C ++ 0x标准(在gnu gcc-4.3中部分可用)引入可用于区分左值和右值引用的右值引用[&& amp;]。通过这种方式,可以实现 move constructor 以返回一个对象,部分地避免了复制构造函数和临时函数的析构函数。



移动构造函数基本上是Andrei在几年前在文章 http://www.ddj.com/database/184403855 具有以下签名:



pre> //移动构造函数
object(object&& obj)
{}

并且它应该获取所传递对象的内部属性的所有权,而后者处于默认状态。通过这样做,避免了内部的副本,临时的破坏变得容易。一个典型的函数工厂将具有以下形式:

  object factory()
{
object obj ;
return std :: move(obj);
}

std :: move()从对象返回一个右值引用。最后但并非最不重要的是,move constructors允许不可复制对象的返回值。


E.g.

What's best out of these:

std::string f() {}

or

const std::string& f() {}

解决方案

A function should never return a reference to a local object/variable since such objects go out of the scope and get destroyed when the function returns.

Differently the function can return a const or non const reference to an object whose scope is not limited by the function context. Typical example is a custom operator<<:

std::ostream & operator<<(std::ostream &out, const object &obj)
{
   out << obj.data();
   return out;
}

Unfortunately returning-by-value has its performance drawback. As Chris mentioned, returning an object by value involves the copy of a temporary object and its subsequent destruction. The copy takes place my means of either copy constructor or operator=. To avoid these inefficiency smart compilers may apply the RVO or the NRVO optimizations, but there are cases in which they can't -- multiple returns.

The upcoming C++0x standard, partially available in gnu gcc-4.3, introduces the rvalue reference [&&] that can be used to distinguish a lvalue from a rvalue reference. By means of that it's possible to implement the move constructor useful to return an object partially avoiding the cost of copy constructor and the destructor of the temporary.

The move constructor is basically what Andrei envisioned some years ago in the article http://www.ddj.com/database/184403855 suggested by Chris.

A move constructor has the following signature:

// move constructor
object(object && obj)
{}

and it's supposed to take the ownership of the internals of the passed object leaving the latter in a default state. By doing that copies of internals are avoided and the destruction of the temporary made easy. A typical function factory will then have the following form:

object factory()
{
    object obj;
    return std::move(obj);
}

The std::move() returns a rvalue reference from an object. Last but not least, move constructors allow the return-by-rvalue-reference of non-copyable objects.

这篇关于它是更有效的返回一个const引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆