I cannot see the need for auto_ptr?!

L

lilburne

Julián Albo said:
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::eek:perator=(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.
 
R

Rob Williscroft

lilburne wrote in
Well I thought "maybe he isn't clueless", and decided to put
it to the test. I tried gcc 3.3.1 which is supposed to have
NVRO and you know what ... it copies the data.

Unfortunatly I can't respond to that as you provided no code and
I don't currently have access to gcc 3.3.1.

#include <iostream>
#include <vector>

using namespace std;

struct test
{
test() { cerr << "Construct\n"; }
test(test const &) { cerr << "Copy\n"; }
~test() { cerr << "Destructor\n"; }

test &operator=(test const &) { cerr << "Assign\n"; return *this; }
};

vector< test > nrv_test()
{
vector< test > nrv;
nrv.push_back( test() );
cerr << "Return\n";
return nrv;
}

int main()
{
vector< test > a = nrv_test() ;
cerr << "Returned\n";
}

I get:

$ g++ -ansi -pedantic test.cpp -o 1.exe

$ 1
Construct
Copy
Destructor
Return
Returned
Destructor

$ g++ --version
g++.exe (GCC) 3.2.3 (mingw special 20030504-1)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Rob.
 
L

lilburne

Julián Albo said:
lilburne escribió:




The second call is an assignment, not an initialization. Initializtion
is not subject of that optimization.

That was the whole point of why someone would use a pointer
rather than an object, which could involve the copying of a
whole mess of data. Some one suggested NVRO was a solution,
it clearly is not.
 
A

Alf P. Steinbach

#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::eek:perator=(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.
 
L

lilburne

Alf said:
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.

That's what I said. But sometimes its worth humouring
someone for awhile.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

lilburne escribió:
As you can see the second call to create() results in the
copying (assignment). The NVRO optimization is minimal and I

The second call is an assignment, not an initialization. Initializtion
is not subject of that optimization.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,147
Latest member
CarenSchni
Top