return-value-optimization

Does RVO work on object members?

南楼画角 提交于 2019-12-18 07:35:09
问题 Consider the following: struct A { /* ... */ }; A foo() { auto p = std::make_pair(A{}, 2); // ... do something return p.first; } auto a = foo(); Will p.first be copied, moved or RVO-ed? 回答1: I've found in Visual Studio 2010 and in gcc-5.1 RVO is not applied (see for example http://coliru.stacked-crooked.com/a/17666dd9e532da76). The relevant section of the standard is 12.8.31.1 [class.copy]. It states that copy elision is permitted (my highlighting): in a return statement in a function with a

Can a C++ compiler perform RVO for a const return value?

随声附和 提交于 2019-12-18 05:55:19
问题 Let's say I have the function #include <string> std::string const foo() { std::string s = "bar"; return s; } int main() { std::string t = foo(); } Can a compiler perform (named) return-value optimization for t , even though the types of s and t are both different from the return type of foo due to the const -ness difference? (If the answer is different for C++03 and C++11 then I'm definitely interested in knowing the C++03 answer.) 回答1: There is no way for RVO optimization to break the

Can a C++ compiler perform RVO for a const return value?

非 Y 不嫁゛ 提交于 2019-12-18 05:55:10
问题 Let's say I have the function #include <string> std::string const foo() { std::string s = "bar"; return s; } int main() { std::string t = foo(); } Can a compiler perform (named) return-value optimization for t , even though the types of s and t are both different from the return type of foo due to the const -ness difference? (If the answer is different for C++03 and C++11 then I'm definitely interested in knowing the C++03 answer.) 回答1: There is no way for RVO optimization to break the

Is returning with `std::move` sensible in the case of multiple return statements?

混江龙づ霸主 提交于 2019-12-17 10:41:27
问题 I'm aware that it's normally not a good idea to return with std::move , i.e. bigObject foo() { bigObject result; /*...*/ return std::move(result); } instead of simply bigObject foo() { bigObject result; /*...*/ return result; } because it gets in the way of return value optimization. But what in the case of a function with multiple different returns, particularly something like class bar { bigObject fixed_ret; bool use_fixed_ret; void prepare_object(bigObject&); public: bigObject foo() { if

In C++, is it still bad practice to return a vector from a function?

非 Y 不嫁゛ 提交于 2019-12-17 04:39:13
问题 Short version: It's common to return large objects—such as vectors/arrays—in many programming languages. Is this style now acceptable in C++0x if the class has a move constructor, or do C++ programmers consider it weird/ugly/abomination? Long version: In C++0x is this still considered bad form? std::vector<std::string> BuildLargeVector(); ... std::vector<std::string> v = BuildLargeVector(); The traditional version would look like this: void BuildLargeVector(std::vector<std::string>& result);

c++11 Return value optimization or move? [duplicate]

血红的双手。 提交于 2019-12-16 19:48:47
问题 This question already has answers here : C++11 rvalues and move semantics confusion (return statement) (6 answers) Closed 2 years ago . I don't understand when I should use std::move and when I should let the compiler optimize... for example: using SerialBuffer = vector< unsigned char >; // let compiler optimize it SerialBuffer read( size_t size ) const { SerialBuffer buffer( size ); read( begin( buffer ), end( buffer ) ); // Return Value Optimization return buffer; } // explicit move

disable return-value-optimization for one function

ぃ、小莉子 提交于 2019-12-13 15:33:19
问题 struct X { void * a; void * b; }; X foo( void * u, void * v); foo() is implemented in assembler (i386) address of return value of type X is passed as hidden parameter to foo() if test code is compiled with -O0 the code works as expected if compiled with -O3 segmentation fault happens (return value was optimized out) if compiled with -O3 -fno-elide-constructors the code works as expected again how can the compiler be forced no to add RVO for foo() only (aka not forcing -fno-elide-constructors)

How can I disable c++ return value optimization for one type only?

白昼怎懂夜的黑 提交于 2019-12-13 06:11:00
问题 I have come across the situation where I really do need to execute non-trivial code in a copy-constructor/assignment-operator. The correctness of the algorithm depends on it. While I could disable return value optimisation with a compiler switch, it seems a waste because it's only the one type I need it disabled for, so why should the performance of the whole application suffer? (Not to mention that my company would not allow me to add the switch, anyway). struct A { explicit A(double val) :

What are copy elision and return value optimization?

倖福魔咒の 提交于 2019-12-12 05:02:21
问题 What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction. For a technical overview, see the standard reference. See common cases here. 回答1: Introduction For a technical overview - skip to this answer. For common cases where copy elision occurs - skip to this answer. Copy elision is an optimization implemented by most

What are copy elision and return value optimization?

寵の児 提交于 2019-12-11 12:24:26
问题 What is copy elision? What is (named) return value optimization? What do they imply? In what situations can they occur? What are limitations? If you were referenced to this question, you're probably looking for the introduction. For a technical overview, see the standard reference. See common cases here. 回答1: Introduction For a technical overview - skip to this answer. For common cases where copy elision occurs - skip to this answer. Copy elision is an optimization implemented by most