return-value-optimization

Copy constructor vs. return value optimization

In a previous question, it appeared that a plain return-by-value function always copies its return argument into the variable being assigned from it. Is this required by the standard, or can the function be optimized by constructing the 'assigned to' variable even within the function body? struct C { int i; double d; }; C f( int i, in...

Return value optimization in VC2008

Is there other technique like RVO (return value optimization) or NRVO (named return value optimization) that can be use with VC2008? ...

Are return values going to be passed by rvalue reference in c++0x?

Let's say I have a function: typedef std::vector<int> VecType; VecType randomVector(); int processing() { VecType v = randomVector(); return std::accumulate(v.begin(), v.end(), 0); } Does C++0x specifically say the spurious copy will be averted from the return value of randomVector? Or would a compiler need to implement the R...

Understanding return value optimization and returning temporaries - C++

Please consider the three functions. std::string get_a_string() { return "hello"; } std::string get_a_string1() { return std::string("hello"); } std::string get_a_string2() { std::string str("hello"); return str; } Will RVO be applied in all the three cases? Is it OK to return a temporary like in the above code? I ...

When should RVO kick-in?

From the following code, If RVO has happened, I expect to see the 2 addresses pointing to the same location, however this is not the case (my compiler is MS VC9.0) #include <iostream> #include <string> std::string foo(std::string& s) { std::cout << "address: " << (unsigned int)(&s) << std::endl; return s; } int main() { std::...

Trusting the Return Value Optimization

How do you go about using the return value optimization? Is there any cases where I can trust a modern compiler to use the optimization, or should I always go the safe way and return a pointer of some type/use a reference as parameter? Is there any known cases where the return value optimization cant be made?, Seems to me that the retu...

C++: returning by reference and copy constructors

References in C++ are baffling me. :) The basic idea is that I'm trying to return an object from a function. I'd like to do it without returning a pointer (because then I'd have to manually delete it), and without calling the copy-constructor, if possible (for efficiency, naturally added: and also because I wonder if I can't avoid writi...

Does "Return value optimization" cause undefined behavior?

Reading this Wikipedia article pointed by one of the repliers to the following question: http://stackoverflow.com/questions/2323225/c-copy-constructor-temporaries-and-copy-semantics I came across this line Depending on the compiler, and the compiler's settings, the resulting program may display any of the following outputs: Doesn...

const reference to temporary and copying - C++

Please consider the following code, struct foo { foo() { std::cout << "Constructing!" << std::endl; } foo(const foo& f) { std::cout << "Copy constructing!" << std::endl; } ~foo() { std::cout << "Destructing.." << std::endl; } }; foo get() { foo f; return f; } int ma...

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

Hello! 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 woul...

In C++, is it still bad practice to return a vector from a function?

Short version: It's common to return large objects—such as vectors/arrays—in many programming languages. Is this style now acceptable in C++0x if the class has a move constructor, or do C++ programmers consider it weird/ugly/abomination? Long version: In C++0x is this still considered bad form? std::vector<std::string> BuildLargeVector...

Likeliness of Named RVO?

I have a function that looks like this: // Fetch 1 MB of data void GetData(std::vector<char> & outData); The 1MB is exaggerated, but I just want to make the point that it's preferable to avoid unnecessary copies. If I add this overload: std::vector<char> GetData() { std::vector<char> result; GetData(result); return resul...

Isn't return value optimization (RVO) a bug?

I maybe asking a dumb question, but I looked at the wikipedia page for RVO here and could not stop wondering if that behavior is wrong. I tried it in my machine and RVO is fully kicked in despite optimization level. What if there was actually something BIG happenning in a constructor? I know it shouldn't, but what if? I can't understand ...