Proper way (move semantics) to return a std::vector from function calling in C++11

If you’re using a C++0x-compatible compiler and standard library, you get better performance from the first example without doing anything. The return value of get_vector(_n, _m) is a temporary, and the move constructor for std::vector (a constructor taking an rvalue reference) will automatically be called with no further work on your part. In general, non-library … Read more

Why does returning a std::optional sometimes move and sometimes copy?

Because returning a name of an object with automatic storage duration is treated as returning an rvalue of the object. Note this works only if the expression in the return statement is a (possibly parenthesized, not including braces) name, like return u; or return (u);, so return {u}; works as usual, i.e. the copy constructor … Read more

Why are by-value parameters excluded from NRVO?

Here’s why copy elision doesn’t make sense for parameters. It’s really about the implementation of the concept at the compiler level. Copy elision works by essentially constructing the return value in-place. The value isn’t copied out; it’s created directly in its intended destination. It’s the caller who provides the space for the intended output, and … Read more

Why is public destructor necessary for mandatory RVO in C++?

This is CWG 2426. The destructor is potentially invoked within this context, because even after the initialization of the return A object, it’s still possible that the function fails to complete successfully: any temporaries created during the return statement, and automatic local variables that are in scope, must be destroyed, and if the destruction throws, … Read more

Move or Named Return Value Optimization (NRVO)?

The compiler may NRVO into a temp space, or move construct into a temp space. From there it will move assign x. Update: Any time you’re tempted to optimize with rvalue references, and you’re not positive of the results, create yourself an example class that keeps track of its state: constructed default constructed moved from … Read more

Why does std::move prevent RVO (return value optimization)?

The cases where copy and move elision is allowed is found in section 12.8 ยง31 of the Standard (version N3690): When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side … Read more

Return value optimization and copy elision in C

RVO/NRVO are clearly allowed under the “as-if” rule in C. In C++ you can get observable side-effects because you’ve overloaded the constructor, destructor, and/or assignment operator to give those side effects (e.g., print something out when one of those operations happens), but in C you don’t have any ability to overload those operators, and the … Read more

c++11 Return value optimization or move? [duplicate]

Use exclusively the first method: Foo f() { Foo result; mangle(result); return result; } This will already allow the use of the move constructor, if one is available. In fact, a local variable can bind to an rvalue reference in a return statement precisely when copy elision is allowed. Your second version actively prohibits copy … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)