Optimization of return value vs auto_ptr for large vectors

If I use auto_ptr as the return value of a function that fills large vectors, this makes the function the original function (it will create an internal auto_ptr and transfer ownership when it returns not const const_ppt). However, I cannot use this function with STL algorithms because I need to split auto_ptr to access the data. A good example, I think, would be a field of vectors of size N, with each vector having 100 components. The Wether function returns every 100 components of a vector by value or by ref is not the same if N is large.

Also, when I try this very basic code:

class t
{
    public: 
         t() { std::cout << "ctor" << std::endl; }
         ~t() { std::cout << "dtor" << std::endl; }
};

t valueFun()
{
   return t();
}

std::auto_ptr<t> autoFun()
{
   return std::auto_ptr(new t());
}

and autoFun and fun calls are output

CTOR Dtor

, return. , ​​ valueFun? Does valueFun ?

?

+5
2

, .


: ?

, , ... , .


, ++ 03 : :

  • : RVO Debug gcc, .
  • "out" ( )
  • ( )

, , .

++ 11 , , return, RVO , ( ) ; vector .

:

  • : RVO,
  • unique_ptr

, : sizeof, a std::array<T,10> 10*sizeof(T), + unique_ptr.


Tangent: . , , , / , , , . RVO , , 10 : .;)

+4

, valueFun. , , :

, auto_ptr - .

, (std::vector aCopy(std::move(otherVector)) ++ 11. , RVO, ( , RVO .)

, ( rvalue)

+1

All Articles