rvo

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 the error C2248 when the copy constructor is private in the code below?

时间秒杀一切 提交于 2019-12-25 03:32:59
问题 This code emits error C2248: 'A::A' : cannot access private member declared in class 'A' in VS2010, although RVO doesn't need a copy constructor. To prove this, just turn public the declaration A(const A&); below, and the code will execute without a problem, even without a definition for the copy constructor . class A { int i; A(const A&); public: A() : i(1) {} }; A f() { return A(); } int main() { A a; a = f(); } 回答1: Just because your program doesn't end up actually invoking the copy

Return value optimization: ho can I avoid copy construction of huge STL containers.

这一生的挚爱 提交于 2019-12-23 09:59:12
问题 When I want a function to return me a container: vector<T> func(){ vector<T> result; ... return result; } To be used in the following way: vector<T> result = func(); In order to avoid the overhead of copying my container I often write the function so that it returns nothing but accept a non-const instance of the container. void func(vector<T>& result){ result.clear(); ... result; } To be used in the following way: vector<T> result; func(result); Is my effort meaningless because I can be sure

Why doesn't RVO happen for assignment operator? (C++)

爷,独闯天下 提交于 2019-12-23 02:44:12
问题 Example: A myfunction() { return A(); } A a = myfunction(); // default ctor only (return value optimization) a = myfunction(); // default ctor and operator= Why can't the compiler just write the new object into the existing object? I believe all instances of a class occupy the same amount of (non dynamic) memory, so I don't see why this would be a problem. 回答1: RVO happens only because the C++ standard gives compilers a special license to ignore side effects in the copy constructor and the

Is the object copied or not when RVO/NRVO kicks in?

喜欢而已 提交于 2019-12-22 06:49:19
问题 I can't get my head around RVO (and NRVO) definition because of multiple questions like this one that to me look assuming that RVO omits a copy constructor. Now according to 12.8.15 In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. Which looks like it

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

拜拜、爱过 提交于 2019-12-19 16:06:13
问题 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 回答1: I

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)

Is a move constructor/assignment needed for RVO to kick in in C++11?

倖福魔咒の 提交于 2019-12-11 16:12:55
问题 For example: In accepted answer https://stackoverflow.com/a/14623480/1423254, Does copy elision and RVO would still work for classes without move constructors? Yes, RVO still kicks in. Actually, the compiler is expected to pick: RVO (if possible) In accepted answer https://stackoverflow.com/a/38043447/1423254, Under non-guaranteed copy elision rules, this will create a temporary, then move from that temporary into the function's return value. That move operation may be elided, but T must

C++ returning an object copy

二次信任 提交于 2019-12-10 13:23:22
问题 I wrote the following code: class MyObjectHolder { public: std::vector<int> getMyObject() const { return myObject; } private: std::vector<int> myObject; }; At some point of my program I attempt to use the getMyObject method and use only const methods on the retrieved object: const std::vector<int> myObject = myObjectHolder.getMyObject(); myObject.size(); int a = myObject.front(); Now, is it possible that the compiler will optimize this code so that no copies of the std::vector<int> are done?