return-value-optimization

Is RVO (Return Value Optimization) on unnamed objects a universally guaranteed behavior?

﹥>﹥吖頭↗ 提交于 2020-01-29 13:33:36
问题 This question is in different aspect (also limited to gcc). My question is meant only for unnamed objects . Return Value Optimization is allowed to change the observable behavior of the resulting program. This seems to be mentioned in standard also. However, this "allowed to" term is confusing. Does it mean that RVO is guaranteed to happen on every compiler. Due to RVO below code changes it's observable behavior: #include<iostream> int global = 0; struct A { A(int *p) {} A(const A &obj) { ++

Is RVO (Return Value Optimization) on unnamed objects a universally guaranteed behavior?

梦想与她 提交于 2020-01-29 13:32:55
问题 This question is in different aspect (also limited to gcc). My question is meant only for unnamed objects . Return Value Optimization is allowed to change the observable behavior of the resulting program. This seems to be mentioned in standard also. However, this "allowed to" term is confusing. Does it mean that RVO is guaranteed to happen on every compiler. Due to RVO below code changes it's observable behavior: #include<iostream> int global = 0; struct A { A(int *p) {} A(const A &obj) { ++

Does returning by const value affect return value optimization? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 08:46:09
问题 This question already has answers here : Can a C++ compiler perform RVO for a const return value? (1 answer) Purpose of returning by const value? [duplicate] (4 answers) Closed 5 years ago . Consider the function const std::string f() { return "hello"; } And the call std::string x = f(); Regardless of whether value return types should be const or not, does the fact the return value is const, prevent a compiler from performing return value optimization? My understanding of RVO is that the

Why is Visual C++ not performing return-value optimization on the most trivial code?

只谈情不闲聊 提交于 2020-01-01 04:17:05
问题 Does Visual C++ not perform return-value optimization? #include <cstdio> struct Foo { ~Foo() { printf("Destructing...\n"); } }; Foo foo() { return Foo(); } int main() { foo(); } I compile and run it: cl /O2 test.cpp test.exe And it prints: Destructing... Destructing... Why is it not performing RVO? 回答1: When I test with this: #include <iostream> struct Foo { Foo(Foo const &r) { std::cout << "Copying...\n"; } ~Foo() { std::cout << "Destructing...\n"; } Foo() {} }; Foo foo() { return Foo(); }

Copy constructor not called when initializing an object with return value of a function

北城以北 提交于 2019-12-31 22:23:41
问题 Consider the following code: #include <iostream> using namespace std; class A { public: int a; A(): a(5) { cout << "Constructor\n"; } A(const A &b) { a = b.a; cout << "Copy Constructor\n"; } A fun(A a) { return a; } }; int main() { A a, c; A b = a.fun(c); return 0; } The output of the above code with g++ file.cpp is: Constructor Constructor Copy Constructor Copy Constructor The output of the above code with g++ -fno-elide-constructors file.cpp is: Constructor Constructor Copy Constructor Copy

const reference to temporary vs. return value optimization

与世无争的帅哥 提交于 2019-12-31 10:34:16
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

const reference to temporary vs. return value optimization

雨燕双飞 提交于 2019-12-31 10:34:12
问题 I'm aware of the fact that assigning an rvalue to a const lvalue reference extends the temporaries lifetime until the end of the scope. However, it is not clear to me when to use this and when to rely on the return value optimization. LargeObject lofactory( ... ) { // construct a LargeObject in a way that is OK for RVO/NRVO } int main() { const LargeObject& mylo1 = lofactory( ... ); // using const& LargeObject mylo2 = lofactory( ... ); // same as above because of RVO/NRVO ? } According to

Returning std::move of a local variable [duplicate]

♀尐吖头ヾ 提交于 2019-12-24 17:04:56
问题 This question already has answers here : Using std::move() when returning a value from a function to avoid to copy (3 answers) Closed 6 years ago . Let there be a class A with a move constructor. Consider this: A get() { A a; return std::move( a ); } // later in the code A aa = get(); Here the explicit call to std:move forces the move constructor of A to be called thus it might inhibit the return value optimization in while calling get() . Thus it is said the a better implementation of get()

C++ get method - returning by value or by reference

大兔子大兔子 提交于 2019-12-21 05:06:45
问题 I've go a very simple question, but unfortunately I can't figure the answer myself. Suppose I've got some data structure that holds settings and acts like a settings map. I have a GetValue(const std::string& name) method, that returns the corresponding value. Now I'm trying to figure out - what kind of return-value approach would be better. The obvious one means making my method act like std::string GetValue(const std::string& name) const and return a copy of the object and rely on RVO in

Efficient use of move semantics together with (N)RVO

泄露秘密 提交于 2019-12-20 09:46:01
问题 Let's say I want to implement a function that is supposed to process an object and return a new (possibly changed) object. I would like to do this as efficient as possible in C+11. The environment is as follows: class Object { /* Implementation of Object */ Object & makeChanges(); }; The alternatives that come to my mind are: // First alternative: Object process1(Object arg) { return arg.makeChanges(); } // Second alternative: Object process2(Object const & arg) { return Object(arg)