Re: I cannot see the need for auto_ptr?!
From: Alf P. Steinbach (alfps_at_start.no)
Date: 10/21/03
- Next message: Julián Albo: "Re: I cannot see the need for auto_ptr?!"
- Previous message: red floyd: "Re: vector<int> fillVector(int arguments ...)"
- In reply to: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Next in thread: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Reply: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Julián Albo: "Re: I cannot see the need for auto_ptr?!"
- Previous message: red floyd: "Re: vector<int> fillVector(int arguments ...)"
- In reply to: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Next in thread: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Reply: lilburne: "Re: I cannot see the need for auto_ptr?!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|