temporary object creation question

  • Thread starter xllx.relient.xllx
  • Start date
X

xllx.relient.xllx

Assuming compiler optimizations are set to off, specifically to not
allow the compiler to elide the copy constructor, would the following
hold true?:

If you call a function with an user-defined object by value as it's
first and only argument, and this object was created before the
function call (named object), would the compiler call the copy
constructor to create a temporary object and initialize this object
with the formal argument (what's being passed) and then, another copy
constructor call would be made to initialize the actual argument object
in the function definition with the previous temporary object? In
essense, would two copy constructor calls be made in the situation
presented or would only one copy constructor be called and if so, why?
and.., is returning an object (by value also) any different from
passing?

code:

void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}
 
V

Victor Bazarov

Assuming compiler optimizations are set to off, specifically to not
allow the compiler to elide the copy constructor,

Just to let you know, nothing of the kind is defined by the language.
The Standard allows to forgo creating of the temporaries, but says
noting about optimizations' being "set to off".
would the following
hold true?:

If you call a function with an user-defined object by value as it's
first and only argument, and this object was created before the
function call (named object), would the compiler call the copy
constructor to create a temporary object and initialize this object
with the formal argument (what's being passed) and then, another copy
constructor call would be made to initialize the actual argument
object in the function definition with the previous temporary object?

No, why would it create the temporary? The argument (function-local
object) is copy-initialised from the named object.
In essense, would two copy constructor calls be made in the situation
presented or would only one copy constructor be called and if so, why?

No, only one.
and.., is returning an object (by value also) any different from
passing?

Yes, in most cases a temporary has to be created when returning.
code:

void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}

V
 
F

Frederick Gotham

void theCall(Foo foo) {
// ...
}

int main(int, char**)
{
Foo foo;
theCall(foo);
}


Two objects.

The local object "foo" in "main", which is default-constructed.

The local object "foo" in "theCall", which is copy-constructed.
 

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

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top