return-value-optimization

What is the effect of 'explicit' keyword on the Return Value Optimization (RVO)?

Deadly 提交于 2019-12-11 02:07:25
问题 Following code works perfectly fine (showing RVO): struct A { A (int) { cout << "A::A()\n"; } // constructor A (const A&) { cout << "A::A(const A&)\n"; } // copy constructor }; A foo () { return A(0); } int main () { A a = foo(); } Output: A::A() // --> which means copy constructor is not called If I mark the copy constructor as explicit : explicit A (const A&) { ... } Then the compiler errors out: explicit.cpp: In function ‘A foo()’: explicit.cpp:10:22: error: no matching function for call

Should I assign a ref or a copy to a value returning function?

谁说我不能喝 提交于 2019-12-10 18:21:41
问题 We have a bunch of value returning functions: Foo function_1(){ Foo f; // ... return f; } Bar function_2(){ Bar b; // ... return b; } Baz function_3(){ Baz b; // ... return b; } I'm using them to create instantiations of local variables: void example(){ //... const auto foo = function_1(); //... const auto bar = function_2(); //... const auto baz = function_3(); } However, my teammates keep asking me to convert all my instantiations to use & : void example(){ //... const auto& foo = function

Why the copy constructor is not called?

吃可爱长大的小学妹 提交于 2019-12-10 01:59:13
问题 In this code: #include <iostream> using std::cout; class Foo { public: Foo(): egg(0) {} Foo(const Foo& other): egg(1) {} int egg; }; Foo bar() { Foo baz; baz.egg = 3; return baz; } int main(void) { Foo spam(bar()); cout << spam.egg; return 0; } the output is 3 , while I expected it to be 1 . That means the copy constructor is not called in the line Foo spam(bar()) . I guess it's because the bar function doesn't return a reference. Could you please explain what's really going on at the

std::move and RVO optimizations

核能气质少年 提交于 2019-12-05 00:53:07
问题 I've recently read how std::move can speed up code by just moving the values instead of copying them. So I made a test program to compare the speed using std::vector . The code: #include <iostream> #include <vector> #include <stdint.h> #ifdef WIN32 #include <Windows.h> #else #include <sys/time.h> #include <ctime> #endif #undef max // Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both // windows and linux. uint64_t GetTimeMs64() { #ifdef _WIN32 // Windows FILETIME

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

与世无争的帅哥 提交于 2019-12-04 02:28:29
This question already has answers here : Closed 5 years ago . Can a C++ compiler perform RVO for a const return value? (1 answer) Purpose of returning by const value? [duplicate] (4 answers) 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 returned object is constructed directly into the variable outside the function. However, if the return type

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

烈酒焚心 提交于 2019-12-03 15:59:20
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 performance meanings. The other one would mean making two methods std::string& GetValue(...) const std:

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

半世苍凉 提交于 2019-12-03 03:03:29
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 Constructor Copy Constructor I know Return Value Optimization. My question is which call to copy

const reference to temporary vs. return value optimization

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 20:32:24
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 Scot Meyers' More Effective C++ (Item 20) the second method could be optimized by the compiler to

Efficient use of move semantics together with (N)RVO

可紊 提交于 2019-12-02 19:49:31
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).makeChanges(); } Object process2(Object && arg) { return std::move(arg.makeChanges()); } // Third alternative:

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 06:07:10
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) { ++ global; } }; A foo () { return A(0); } // <--- RVO happens int main () { A obj = foo(); std::cout<<