rvo

g++: How RVO works in case that multiple translation units are involved

…衆ロ難τιáo~ 提交于 2019-12-08 15:42:41
问题 Firstly please take a look at the following code, which consists of 2 translation units. --- foo.h --- class Foo { public: Foo(); Foo(const Foo& rhs); void print() const; private: std::string str_; }; Foo getFoo(); --- foo.cpp --- #include <iostream> Foo::Foo() : str_("hello") { std::cout << "Default Ctor" << std::endl; } Foo::Foo(const Foo& rhs) : str_(rhs.str_) { std::cout << "Copy Ctor" << std::endl; } void Foo:print() const { std::cout << "print [" << str_ << "]" << std:endl; } Foo getFoo

Will RVO happen when returning std::pair?

[亡魂溺海] 提交于 2019-12-07 00:09:20
问题 A function needs to return two values to the caller. What is the best way to implement? Option 1: pair<U,V> myfunc() { ... return make_pair(getU(),getV()); } pair<U,V> mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) = myfunc(); Option 2: void myfunc(U& u , V& v) { u = getU(); v= getV(); } U u; V v; myfunc(u,v); I know with Option2, there are no copies/moves but it looks ugly. Will there be any copies/moves occur in Option1, 1.1? Lets assume U and V are huge objects supporting

Will RVO happen when returning std::pair?

扶醉桌前 提交于 2019-12-05 04:52:01
A function needs to return two values to the caller. What is the best way to implement? Option 1: pair<U,V> myfunc() { ... return make_pair(getU(),getV()); } pair<U,V> mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) = myfunc(); Option 2: void myfunc(U& u , V& v) { u = getU(); v= getV(); } U u; V v; myfunc(u,v); I know with Option2, there are no copies/moves but it looks ugly. Will there be any copies/moves occur in Option1, 1.1? Lets assume U and V are huge objects supporting both copy/move operations. Q: Is it theoretically possible for any RVO/NRVO optimizations as per the

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

Will C++ compiler optimize return-by-value code?

眉间皱痕 提交于 2019-12-01 19:44:58
问题 Lets assume i use Visual Studio or modern GCC with -O2. Will compiler create S inside func() and then copy it to a my_result , or will it create my_result with constructor (5, 6, 5 + 6) without creating temporary S ? NOTE: Function func() definition and its usage are in separate .obj files! struct S { S(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } int x, y, z; }; S func(int a, int b) { return S(a, b, a + b); } /// USAGE /// S my_result = func( 5, 6 ); 回答1: Modern compilers will often

C++ universal reference in constructor and return value optimization (rvo)

狂风中的少年 提交于 2019-12-01 16:55:42
Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include <iostream> template<class ...ArgsIn> struct C { template<class ...Args> C(Args&& ... args) {std::cout << "Ctr\n";} // rvo occurs without && ~C(){std::cout << "Dstr\n";} }; template<class ...Args> auto f(Args ... args) { int i = 1; return C<>(i, i, i); } int main() { auto obj = f(); } Output: Ctr Ctr Dstr Ctr Dstr Dstr I believe that the problem is that instantiations of template<class ...Args> C(Args&& ... args) {std::cout << "Ctr

Multiple return values (structured bindings) with unmovable types and guaranteed RVO in C++17

耗尽温柔 提交于 2019-11-29 12:16:42
问题 With C++ 17, we will have the possibility to return unmovable (including uncopyable) types such as std::mutex , via what can be thought of as guaranteed return value optimization (RVO): Guaranteed copy elision through simplified value categories: struct nocopy { nocopy(nocopy&) = delete; nocopy() = default; }; auto getRVO(){ return nocopy(); } We will also have structured bindings, allowing: tuple<T1,T2,T3> f(); auto [x,y,z] = f(); or (here also using my understanding of the feature template

Why g++ does not enable RVO here?

故事扮演 提交于 2019-11-29 08:52:52
Consider that TEST code: #include <iostream> using namespace std; class Klass { public: Klass() { cout << "Klass()" << endl; } Klass(const Klass& right) { cout << "Klass(const Klass& right)" << endl; } }; Klass create(Klass a) { cout << "create(Klass a)" << endl; return a; } int main() { const Klass result = create(Klass()); } Compiling with: g++ -O3 rvo.cpp -o rvo The output is: $ ./rvo Klass() create(Klass a) Klass(const Klass& right) I was expecting the compiler to use the RVO mechanism in order elide every COPY CTOR call, to avoid copying the return value AND the parameter of the function

Timing of scope-based lock guards and return values

情到浓时终转凉″ 提交于 2019-11-28 08:06:05
问题 class C { mutable std::mutex _lock; map<string,string> deep_member; public: auto get_big_lump() { std::unique_lock<std::mutex> lock(_lock); // establish scope guard return deep_member; // copy the stuff while it can't be changed on another thread. } }; What is the guaranteed timing with respect to the guard and the copying of the return value? Will the copy take place while the lock is held , or can some of it be done after the function body returns, in the case of allowed (or actual!)

Why does Visual Studio not perform return value optimization (RVO) in this case

旧城冷巷雨未停 提交于 2019-11-28 05:42:16
I was answering a question and recommending return by-value for a large type because I was confident the compiler would perform return-value optimization (RVO) . But then it was pointed out to me that Visual Studio 2013 was not performing RVO on my code. I've found a question here regarding Visual Studio failing to perform RVO but in that case the conclusion seemed to be that if it really matters Visual Studio will perform RVO. In my case it does matter, it makes a significant impact to performance which I've confirmed with profiling results. Here is the simplified code: #include <vector>