vector



 DEVELOP > c-Plus-Plus > vector

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Curt Larsson"
Date: 26 Nov 2003 03:21:41 PM
Object: vector
Hi
Can I return a vector from a funktion ?
Like:
std::vector<int> foo();
mvh
Curt
.

User: ".oO LGV Oo."

Title: Re: vector 26 Nov 2003 03:43:24 PM
"Curt Larsson" <kulan@chello.se> a écrit dans le message de
news:uP8xb.107$7U1.1272@amstwist00...

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...
.
User: "Gianni Mariani"

Title: Re: vector 27 Nov 2003 12:35:23 AM
..oO LGV Oo. wrote:

"Curt Larsson" <kulan@chello.se> a écrit dans le message de
news:uP8xb.107$7U1.1272@amstwist00...

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...

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
[
.
User: ".oO LGV Oo."

Title: Re: vector 27 Nov 2003 01:56:23 PM

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.
.
User: "Rob Williscroft"

Title: Re: vector 27 Nov 2003 02:16:37 PM
..oO LGV Oo. wrote in news:bq5kst$390$1@news.tiscali.fr:

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.

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.
--
http://www.victim-prime.dsl.pipex.com/
.

User: "White Wolf"

Title: Re: vector 27 Nov 2003 02:35:43 PM
..oO LGV Oo. wrote:

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.
--
WW aka Attila
:::
Security is an exercise in applied paranoia. -- Unknown
.
User: "lilburne"

Title: Re: vector 27 Nov 2003 05:23:25 PM
White Wolf wrote:


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.

In any case it won't work if the result is being used as an
assignment rather than a initialisation.
.





User: "Paresh Patel"

Title: Re: vector 27 Nov 2003 01:38:35 PM
Yeah you can.
Curt Larsson wrote:

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

std::vector<int> foo();

mvh

Curt







.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER