Re: I cannot see the need for auto_ptr?!

From: Alf P. Steinbach (alfps_at_start.no)
Date: 10/21/03


Date: Tue, 21 Oct 2003 21:02:16 GMT

On Tue, 21 Oct 2003 21:23:51 +0100, lilburne <lilburne@godzilla.net> wrote:

>Julián Albo wrote:
>
>>
>> The output is:
>>
>> a
>> Constructor
>> b
>> Copy constructor
>>
>> No copy construction for a, and the copy constructor clearly works.
>>
>> How do you tested in gcc 3.3.1?
>>
>> Regards.
>
>#include <iostream>
>#include <vector>
>
>using std::cout;
>using std::endl;
>
>class Point {
> public:
> Point();
> Point(const Point& p);
> Point& operator=(const Point& p);
> ~Point();
>};
>
>Point::Point()
>{
> cout << "Point construction" << endl;
>}
>Point::Point(const Point& p)
>{
> cout << "Point copy construction" << endl;
>}
>Point::~Point()
>{
> cout << "Point destructor" << endl;
>}
>
>Point& Point::operator=(const Point& p)
>{
> cout << "Point assignment --- XXXX" << endl;
>}
>
>std::vector< Point > create()
>{
> // populate ...
> std::vector< Point > nrv;
> Point p;
> nrv.push_back(p);
> nrv.push_back(p);
> return nrv;
>}
>
>void f(std::vector< Point > & vec) {
>}
>
>int main()
>{
> cout << "Create start" << endl;
> std::vector< Point > res( create() );
> cout << "Create finished" << endl;
> f(res);
> cout << "Create start" << endl;
> res = create();
> cout << "Create finished" << endl;
> f(res);
> return 0;
>}
>
>Output is:
>Create start
>Point construction
>Point copy construction
>Point copy construction
>Point copy construction
>Point destructor
>Point destructor
>Create finished
>Create start
>Point construction
>Point copy construction
>Point copy construction
>Point copy construction
>Point destructor
>Point destructor
>Point assignment --- XXXX
>Point assignment --- XXXX
>Point destructor
>Point destructor
>Create finished
>Point destructor
>Point destructor
>
>As you can see the second call to create() results in the
>copying (assignment). The NVRO optimization is minimal and I
>can get the same effect as an earlier contributor said by
>using the idiom:
>
> vector < Point > vec;
> create(vector< Point >& vec);
>
>which isn't compiler dependent.

None of you have tested NRVO (Named Return Value Optimization), which,
incidentally, you don't even spell correctly... ;-)

RVO and NRVO are two different things.

In the case of NRVO there is a _named_ return value, which is a
language extension, not part of standard C++.

And just for the record, neither RVO nor NRVO have any bearing on usage
or not of std::auto_ptr.

Hth.



Relevant Pages

  • Exception unwinding base destructor called - why?
    ... construction Derived throws an exception and the handler's unwinding ... mechanism is calling the destructor for the base class. ... Where is the logic in partially deconstructing an aggregate object that's ...
    (comp.lang.cpp)
  • Re: Go ahead. Stop programming. This ensures you from any mistakes.
    ... construction of which fails. ... Both in destructor, which is called by user, and in constructor, ... constructor should clear up all resources if an exception is thrown ... need them - but they help readability. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: throwing exception from constructor
    ... Beware that the finalizer will be called in C# even on exceptional ... thrown in the user defined constructor, the user defined destructor will ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: I cannot see the need for auto_ptr?!
    ... Julián Albo wrote: ... > No copy construction for a, and the copy constructor clearly works. ... Point destructor ... Point assignment --- ...
    (comp.lang.cpp)
  • Re: NRVO and RVO
    ... int main ... RVO rvo; ... copy construction is used to create the return value of MyMethod. ... is a little confusing: why not simply ...
    (microsoft.public.vc.language)