vector

C

Curt Larsson

Hi
Can I return a vector from a funktion ?
Like:

std::vector<int> foo();

mvh

Curt
 
O

.oO LGV Oo.

Curt Larsson said:
Hi
Can I return a vector from a funktion ?
Like:

std::vector<int> foo();

mvh

Curt

sure, but since the copy ctor from the vector class will be called, beware
when you're handling a vector of pointers...
 
G

Gianni Mariani

..oO LGV Oo. said:
sure, but since the copy ctor from the vector class will be called, beware
when you're handling a vector of pointers...


I thought that this was not REALLY true just mostly true. There is one
exception in this case:

std::vector<int> foo();

std::vector<int> variable = foo();

I thought the compiler was free to optimize away the copy constructor
and have the function foo() construct the object directly.

For example:

#include <iostream>

class Yeller
{
int i;
public:

Yeller( int i )
: i( i )
{
std::cout << "Yeller " << i << "\n";
}

Yeller( const Yeller & v )
{
std::cout << "Yeller copy\n";
}
};


Yeller foo()
{
return Yeller( 1 );
}

Yeller boo()
{
return Yeller( 2 );
}

Yeller x = foo();


Yeller koo()
{
return x;
}


int main()
{
Yeller y = boo();

Yeller z( x );

Yeller k = koo();
}


prints (with gcc 3.3.1) :

Yeller 1
Yeller 2
Yeller copy
Yeller copy
[
 
O

.oO LGV Oo.

I thought the compiler was free to optimize away the copy constructor
and have the function foo() construct the object directly.

yep, in the case of the "return optimization", which is :

instead of writing something like :

Object foo()
{
Object o(params);
return o;
}

you write :

Object foo()
{
return Object(params);
}

in the last case, the instance of Object is direcly build as the return
value, so, no copy constructor is called.
 
R

Rob Williscroft

..oO LGV Oo. wrote in
yep, in the case of the "return optimization", which is :

instead of writing something like :

Object foo()
{
Object o(params);
return o;
}

you write :

Object foo()
{
return Object(params);
}

in the last case, the instance of Object is direcly build as the return
value, so, no copy constructor is called.

The former case is allowed too, its often refered to as NRVO, Named
Return Value Optimisation. There are less compilers out there that
currently support NRVO, gcc (3.2.3 to my knowledge ) and I assume all
the EDG based compilers (Comeau, forthcoming borland bccx ...).

Rob.
 
W

White Wolf

..oO LGV Oo. said:
I thought the compiler was free to optimize away the copy constructor
and have the function foo() construct the object directly.
[SNIP]
in the last case, the instance of Object is direcly build as the
return value, so, no copy constructor is called.

IF (big big if) the optimization is present in the compiler. It is not
required to be.
 

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

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top