nrvo

Why NRVO is not applied here?

痴心易碎 提交于 2020-01-22 20:31:12
问题 NRVO is not applied when I run this code in VS2010. #include <stdio.h> class A { public: A() { printf( "I am in constructor\n" ); } A(const A& a) { printf( "I am in copy constructor\n" ); } ~A() { printf( "I am in destructor\n" ); } int i; }; A f(int j) { A a; if ( j ) return a; a.i = j; return a; } int main() { A a; a = f(5); } Edit: this has something to do with the destructor. When I comment out its line, NRVO is used. But why is this? 回答1: Why NRVO is not applied here? If this purely a

C++ nrvo/copy elision with return statement in parentheses

只谈情不闲聊 提交于 2020-01-04 03:29:05
问题 i was fooling around with the following code and got different results using my visual studio 2017 application and two different online compilers. in release mode visual studio does elide the copy/move in both cases, while the two online compilers just do it in case of the unparenthesized return statement. my question is: who is right and more importantly what are the underlaying rules. (i know you can use the parentheses in conjunction with the decltype(auto) syntax. but this is not 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

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

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)

Why doesn't NRVO work without copy constructor

时间秒杀一切 提交于 2019-12-13 06:59:34
问题 I'm reading the book Inside C++ Object Model and I got things below: As I know, NRVO won't call the copy constructor of the class that a function returns.So I don't understand why "This first verison of the program does not apply NRV optimization because of the absence of a copy constructor...". 回答1: The idea is that the code should work both for compilers that perform the optimization and for those that don't. If the object is copied, obviously the copy constructor must be defined and

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++NRVO guarantees? Or better prefer non-const ref param or shared_ptr?

主宰稳场 提交于 2019-12-08 00:41:15
问题 I have been using C++ since 1992 (and reading copious amounts about the language), so I know a fair amount about the language, but far from all. My question is about C++11 named return value optimization - what guarantees are there that it will be performed? I tend to prefer to send in non-const parameters (C++97 style) or use shared_ptr (C++11 style), or even used ptr-to-ptr (C style). One reason is that with non-const ref args or shared_ptr, I am guaranteed that NO extra object copies are

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