C ++引用和返回值 [英] C++ references vs return values

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

问题描述

我理解引用的原则是避免制作大型结构的副本,但如果你正在写的函数本身创建了一个大型结构呢?在本地创建变量,然后返回它,而不是传递目标对象作为参考,并从函数内部填充它是效率较低(或者你更有可能用尽内存)。



我看起来不太好,所以一个具体的例子:假设一个函数接受一个字符串,并返回一个向量的每一行的字符串。对函数有一个物质优势:

  void getLines(std :: string in,std :: vector< std :: string>& out); 

 code> std :: vector< std :: string> getLines(std :: string in);感谢您的帮助,
Wyatt

=h2_lin>解决方案

第一个例子应该更像是这两个例子之一:

  void getLines(std :: string in,std :: vector< std :: string>& out); 
void getLines(std :: string in,std :: vector< std :: string> * out);

第二种方式通常更方便,在理论上可以做得和第一种方式:



http://en.wikipedia.org/wiki/Return_value_optimization



在实践中是否这样看取决于编译器,优化级别,以及编译器是否可以首先发现机会。



虽然我不是为每个人说话,我从来没有设法让编译器利用RVO,即使有简单的对象,不做任何想法。所以我个人建议第一种方法,因为你保证获得所需的行为,在每个编译器,和(这似乎与我相关...)对于每个程序员的代码 - 即,创建的对象保持返回值由被调用的函数直接填充,没有任何额外的副本。



(默认构造结果对象的成本,这是可能避免的东西RVO被利用,通常不是重要的相比,编译器可能无法避免。)



另一点要注意的是,如果试图避免不必要的副本,通过const引用而不是通过值传递对象通常是一个好主意,例如:

  void getLines(const std: :string& in,std :: vector< std :: string>& out); 
void getLines(const std :: string& in,std :: vector< std :: string> * out);


I understand the principle point of references is to avoid making copies of large structures, but what if a function you're writing itself creates a large structure? Is it less efficient (or are you more likely to run out of memory) to create the variable locally, then return it, than you are to pass the destination object as a reference, and fill it from within the function?

I can't seem to phrase that well, so a concrete example: Suppose a function takes a string, and returns a vector of each line in the string. Is there a material advantage to the function:

void getLines(std::string in, std::vector<std::string>& out);

over:

std::vector<std::string> getLines(std::string in);

Thanks for any help, Wyatt

解决方案

The first example should presumably be more like one of these two:

void getLines(std::string in, std::vector<std::string> &out);
void getLines(std::string in, std::vector<std::string> *out);

The second style is often more convenient, and can in theory be made mostly as efficient as the first one:

http://en.wikipedia.org/wiki/Return_value_optimization

Whether this is seen in practice depends on the compiler, optimization levels, and whether the compiler can spot the opportunity in the first place.

Whilst I don't speak for everybody, I've never actually managed to get compilers to take advantage of RVO, even with simple objects that don't do anything fancy. So I therefore personally recommend the first approach, because you are guaranteed to get the desired behaviour, on every compiler, and (which seems relevant for me...) for every programmer's code -- i.e., that the object created to hold the return value is filled in directly by the called function, without any extra copies.

(The cost of default-constructing the result object, which is something that could potentially be avoided were RVO to be taken advantage of, is usually not significant compared to the copy the compiler might be unable to avoid.)

Another point to note would be that if attempting to avoid unnecessary copies, passing objects by const reference instead of by value is often a good idea, e.g.:

void getLines(const std::string &in, std::vector<std::string> &out);
void getLines(const std::string &in, std::vector<std::string> *out);

这篇关于C ++引用和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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