473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

argument passing by value and temporaries

Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

....

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
So copy .ctor is used two times here ?

In the second case X() expression that is rvalue of type X.. I'm not
sure as well about the terminology to use here... X() expression is not
temporary object right? it is just a rvalue(that is value initialized)
and temporary object of type X is *copy* constructed with that rvalue
and then 'b' object is also copy constructed from that temporary...

Same holds true for returning objects ?

Now, if everything holds true, implementators can avoid two phase
copying, like in first example instead to copy construct temporary with
'a' object and then to copy construct 'b' object with that temporary,
'b' object is directly copy constructed with the 'a' object?

And in second case X() doesn't create temporary but directly copy
constructs 'b' ?

TIA.

--
David

Dec 14 '06 #1
12 2545

da*****@mail.ru wrote:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.

--
Ivan
http://www.0x4849.net

Dec 14 '06 #2

Ivan Novick wrote:
da*****@mail.ru wrote:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.

--
Ivan
http://www.0x4849.net
When I was searching NG for this type of question that IMHO has been
asked many times, I saw in few of the posts that said temporaries are
created when passing/returning arguments by value.. I can't find these
posts right now, but does it matter anyways? I didn't say I read it
from standard, I just asked if it was really defined in standard that
way... Sorry for being unclear in my first post if it was understood
like that..

And can you provide a verse in the standard where it doesn't require so
? Or any other statement that implies that temporaries are not made
during argument passing/returing by value. I'm really confused about
that matter and I'm in need of clear explaination of standard maybe
with examples too...

Thank you in advance.

--
David

Dec 14 '06 #3

"Ivan Novick" <iv**@0x4849.netwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...
>
da*****@mail.ru wrote:
>Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?
Well, it does create a temporary object, but that object is the parameter
itself. It doesn't create a temporary and then create the parameter from
that.
As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.
yep.

-Howrad
Dec 14 '06 #4
Howard wrote:
"Ivan Novick" <iv**@0x4849.netwrote in message
news:11**********************@t46g2000cwa.googlegr oups.com...

da*****@mail.ru wrote:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}

In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

Well, it does create a temporary object, but that object is the parameter
itself. It doesn't create a temporary and then create the parameter from
that.
As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.

yep.

-Howrad
Howard,

Can you clarify what do you mean when you say "Well, it does create a
temporary object, but that object is the parameter itself" Are you
saying that parameters are temporary objects that doubles up my
confusion ?

At any rate can anybody provide clear definition, maybe quotation(s)
from standard that answers my questions?

TIA.

--
David

Dec 14 '06 #5
>In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is
itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?

Well, it does create a temporary object, but that object is the parameter
itself. It doesn't create a temporary and then create the parameter from
that.
As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.

yep.

-Howrad

Howard,

Can you clarify what do you mean when you say "Well, it does create a
temporary object, but that object is the parameter itself" Are you
saying that parameters are temporary objects that doubles up my
confusion ?
When passing an object by value, the object specified in the function call
(i.e., being "passed") is actually copied into a temporary object which is
created in automatic memory (or "on the stack", if you prefer). Inside the
called function, any reference to the parameter (i.e, any use of its name in
the source code) refers to that temporary object. So any changes to the
parameter object inside that function actually changes the temporary object
that was created. After the function returns, the temporary is destroyed
and those changes are lost.

If the object is passed by reference, however, then any use of the parameter
inside the function is actually affecting the object that was specified when
calling the function, not a temporary copy made from it.

-Howard


Dec 14 '06 #6

Howard wrote:
In the first case temporary variable is created that is *copy*
constructed with 'a' object and upon function call 'b' object is
itself
copy constructed with that temporary variable.. am I doing right
assumtions iff (When parameters are passed/returned by value
temporaries are created) ?
I don't believe the C++ standard requires creating a temporary variable
and then using it to initialize the function parameter with the copy
constructor. It should require only initializing the function
parameter with the copy constructor being passed the function argument.
Where did you get the idea that a temporary is created? Can you quote
a verse in the standard?


Well, it does create a temporary object, but that object is the parameter
itself. It doesn't create a temporary and then create the parameter from
that.

As for actual implementation, as opposed to the standard, when you run
said code only one default constructor for your initial declarion of
'a', and one copy constructor call when you pass the argument is
actually made in gcc.


yep.

-Howrad
Howard,

Can you clarify what do you mean when you say "Well, it does create a
temporary object, but that object is the parameter itself" Are you
saying that parameters are temporary objects that doubles up my
confusion ?

When passing an object by value, the object specified in the function call
(i.e., being "passed") is actually copied into a temporary object which is
created in automatic memory (or "on the stack", if you prefer). Inside the
called function, any reference to the parameter (i.e, any use of its name in
the source code) refers to that temporary object. So any changes to the
parameter object inside that function actually changes the temporary object
that was created. After the function returns, the temporary is destroyed
and those changes are lost.

If the object is passed by reference, however, then any use of the parameter
inside the function is actually affecting the object that was specified when
calling the function, not a temporary copy made from it.

-Howard
So, what happens in second example?
foo(X()); based on what you said b is directly copy constructed by X()
? Does it hold true when returning by value and when throwing an
exception? for example

X foo() {
X c;
return c;
}

....
X a = foo(); // Is a directly copy constructed by c ? and if so then
what's difference between optimized implementation and not optimized
one if argument passing/returning by value directly copy constructs
objects...

Now as far as exceptions are concerned, as I've read throw T() causes
always temporary object to be created and copy constructed by T() and
in catch clause(s) it is caught by reference or object is copy
constructed by that temporary depending how catch clause is
written(T&,T).. why it is any different from parameter passing by value
or returning by value ? and what kind of a optimization can
implementation offer ? I'm still in state of confusion, and would
appreciate if anybody could shed some light in my understanding of
these concepts.

TIA.

--
David

Dec 14 '06 #7
Howard wrote:
Can you clarify what do you mean when you say "Well, it does create a
temporary object, but that object is the parameter itself" Are you
saying that parameters are temporary objects that doubles up my
confusion ?

When passing an object by value, the object specified in the function call
(i.e., being "passed") is actually copied into a temporary object which is
created in automatic memory (or "on the stack", if you prefer).
Howard, I think you are using a looser definition of 'temporary' than
either Ivan or the Standard (if - as it seems - you are applying it to
automatic variables in general). Section 12.2 covers the creation of
temporary objects, and function parameters aren't in the list.

Ivan, even in your second case you only have 1 copy constructor at
most:
1) Default construct your temporary X.
2) Copy construct your function parameter from the temporary.
As you mentioned the compiler is allowed to default construct the X
directly in-place. If you have g++, play around with the
-fno-elide-constructors switch.

I think the confusion arose because you thought that X() doesn't
construct a temporary variable - it does. From Section 5.2.3:
"... the expression T(x1, x2, ...) is equivalent in effect to the
declaration T t(x1, x2, ...); for
some invented temporary variable t, with the result being the value of
t as an rvalue."

Dec 14 '06 #8
da*****@mail.ru wrote:
>
X foo() {
X c;
return c;
}

...
X a = foo();
Here:
1) c is default constructed
2) the temporarary for the return value is copy-constructed from c
3) a is copy constructed from the temporary.

But the optimisations mean that either or both of the copy
constructions can be eliminated. I expect that you modified your
example from section 12.8.15 of the standard, but in case you didn't:
read that section, it is helpful.
Now as far as exceptions are concerned, as I've read throw T() causes
always temporary object to be created and copy constructed by T()
This *can* happen, but as above, the copy construction may be optimised
away.
in catch clause(s) it is caught by reference or object is copy
constructed by that temporary depending how catch clause is
written(T&,T).. why it is any different from parameter passing by value
or returning by value ?
It isn't any different, except that the temporary is (or would be)
created without the cv qualifiers - this means that you can throw an
rvalue and catch a non-const reference. You wouldn't be able to do this
in normal parameter passing. Other than that, it's like calling a
funcion.
and what kind of a optimization can
implementation offer ?
>From section 12.5 again:
"- in a return statement in a function with a class return type, when
the expression is the name of a non-volatile automatic object with the
same cv-unqualified type as the function return type, the copy
operation can be omitted by constructing the automatic object directly
into the function's return value
- when a temporary class object that has not been bound to a reference
(12.2) would be copied to a class object with the same cv-unqualified
type, the copy operation can be omitted by constructing the temporary
object directly into the target of the omitted copy"

Dec 15 '06 #9

Pete C wrote:
Ivan, even in your second case you only have 1 copy constructor at
most:
Sorry Ivan - I meant to reply to dave, not you!

Dec 15 '06 #10
da*****@mail.ru wrote:
When parameters are passed/returned by value temporaries are
created?
Yes. I just want to add to other replies that it may be useful for
understanding to manually inline the function call. Here's an extended
example illustrating by-value parameter passing and return values:

struct X {
X (int);
friend X operator + (X, X);
};

void foo(X param) {
cout << param;
}
X bar (X param) {
X local = param + X (1);
return local;
}

int main () {
X a (1);
X b (2);

// foo (a);
// -----------------------------
{
// --- Parameter setup
X param (a);
// --- Call
cout << param;
}

// foo (X ());
// -----------------------------
{
X _tmp ();
// --- Parameter setup
X param (_tmp);
// --- Call
cout << param;
}

// X c = bar (a + b);
// -----------------------------

X c; // #1a
{
// --- Evaluate expression
X _tmp (a + b);
// --- Return value setup
X _bar_return; // #2a
// --- Parameter setup
X param = _tmp;
// --- Call
X local = param + X (1);
_bar_return = local; // #2b
// --- Assignment
c = _bar_return; // #1b
}
}

The parameter return mechanism above is a simplification though. To
avoid superfluous constructor calls the actual implementation of #1a
and #1b would be more like this:

// #1a - uninitialized allocation
char _c_flesh [sizeof C];
X& c = *reinterpret_cast <X*(&_c_flesh);

// #1b - placement construction
new (&c) X (_bar_return); // copy construct into c

Similarily for #2a and #2b.
X a = foo(); // Is a directly copy constructed by c ? and if so then
what's difference between optimized implementation and not optimized
one if argument passing/returning by value directly copy constructs
objects.
Ok, let's consider return-value optimization (RVO) for the last
function call. It is very similar, except that the compiler recognizes
that "local" and "_bar_return" can be merged and that the result can be
constructed in c directly:

// X c = bar (a + b);
// -----------------------------

X c;
{
// --- Evaluate expression
X _tmp (a + b);
// --- Return value setup
X& _bar_return = c; // #1
// --- Parameter setup
X param = _tmp;
// --- Call
X& local = _bar_return; // #2
local = param + X (1);
}

The optimization happens at #1 and #2 where "_bar_return" and "local"
now are just aliases for c. In essence the code now is equivalent to:

// X c = bar (a + b);
// -----------------------------

X c;
{
// --- Evaluate expression
X _tmp (a + b);
// --- Parameter setup
X param = _tmp;
// --- Call
c = param + X (1);
}

The standard allows this optimization by not requiring that temporary
objects are created. That means that compliant code should not depend
on constructors being called in a deterministic sequence (e.g. a
certain number of times).

Dec 15 '06 #11
da*****@mail.ru wrote:
Hi, I have just started learning C++ language..
I've read much even tried to understand the way standard says but still
can't get the grasp of that concept.

When parameters are passed/returned by value temporaries are
created?(I'm not touching yet the cases where standard allows
optimizations from the side of implementations to avoid copying)
If so, please quote part of the standard that says that.
Assuming it is true, I can imagine two cases such as:

struct X {
};

void foo(X b);

...

int main(int argc, char* argv[]) {
X a;
foo(a); // first case
foo(X()); // second one
}
Had a chance to check the standard and read all the other posters
comments, here's my conclusion:

// no temporary is created
// (unless you call the parameter itself a temp)
// see sections [5.2.2.4] and [12.2] and [6.6.3]
foo(a);

// a temporary is created to return back to the calling program
return a;

// However the temporary may not actually be created if the
// return value is assigned into an object, and compiler can
// directly copy construct with object being returned.
X b = foo();

// an object is created on the stack and default constructed
// said object is passed by value and used as argument to
// copy constructor essentially this is same case as foo(a)
// but you are creating the object and passing it
foo(X());

I am feeling about 97% confident about above statements so feel free to
shoot at it if you see something wrong.

-----
Ivan
http://www.0x4849.net

Dec 15 '06 #12

Pete C wrote:
da*****@mail.ru wrote:

X foo() {
X c;
return c;
}

...
X a = foo();

Here:
1) c is default constructed
2) the temporarary for the return value is copy-constructed from c
3) a is copy constructed from the temporary.

But the optimisations mean that either or both of the copy
constructions can be eliminated. I expect that you modified your
example from section 12.8.15 of the standard, but in case you didn't:
read that section, it is helpful.
Now as far as exceptions are concerned, as I've read throw T() causes
always temporary object to be created and copy constructed by T()

This *can* happen, but as above, the copy construction may be optimised
away.
in catch clause(s) it is caught by reference or object is copy
constructed by that temporary depending how catch clause is
written(T&,T).. why it is any different from parameter passing by value
or returning by value ?

It isn't any different, except that the temporary is (or would be)
created without the cv qualifiers - this means that you can throw an
rvalue and catch a non-const reference. You wouldn't be able to do this
in normal parameter passing. Other than that, it's like calling a
funcion.
and what kind of a optimization can
implementation offer ?
From section 12.5 again:

"- in a return statement in a function with a class return type, when
the expression is the name of a non-volatile automatic object with the
same cv-unqualified type as the function return type, the copy
operation can be omitted by constructing the automatic object directly
into the function's return value
- when a temporary class object that has not been bound to a reference
(12.2) would be copied to a class object with the same cv-unqualified
type, the copy operation can be omitted by constructing the temporary
object directly into the target of the omitted copy"
Pete, Vidar, Ivan thank you very much it is all clear now.

--
David

Dec 15 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
6
by: Jason Heyes | last post by:
I am interested in the lifetime of a function argument in two cases. They are: 1. void foo(Bar bar); 2. void foo(const Bar &bar); In each case I call foo like so: foo(Bar());
4
by: Rein Anders Apeland | last post by:
Consider the following working code: #include <memory> #include <iostream> void print_ptr( const std::auto_ptr< int > & thePtr = std::auto_ptr< int >() ) {
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
17
by: Summercool | last post by:
I wonder which language allows you to change an argument's value? like: foo(&a) { a = 3 } n = 1 print n
17
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.