473,386 Members | 1,973 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.

References to temporaries and function-calls

struct foo {
int i;
};

int bar(foo& f) {
return f.i++;
}

int main() {
bar(foo());
}

The above code does not compile since you can't bind a reference to a
temporary, you could solve this by using a 'const foo&' parameter
instead but then you have other problems (like trying to change the
value of a const). This much I understand, what I don't understand is
how come this is considered trying to bind a reference to a temporary,
are not all parameters supposed to be evaluated before the function is
executed? And these evaluations should take place in the same scope as
in which the function is called? While the function, including its
parameters, are executed in its own scope?

The way I see things the foo()-part of bar(foo()); should already have
executed (and thus have created a foo-object on the stack) when the
parameter f comes into scope* and thus, from the point of view of
bar() be non-temporary.

Or put another way, I can't quite see the difference between the
following two:
bar(foo());
and
foo f;
bar(f);

Can someone please explain?

* Or is declared, or defined or whatever it is called.

--
Erik Wikström

Feb 15 '07 #1
11 1566
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
struct foo {
int i;

};

int bar(foo& f) {
return f.i++;

}

int main() {
bar(foo());

}

The above code does not compile since you can't bind a reference to a
temporary, you could solve this by using a 'const foo&' parameter
instead but then you have other problems (like trying to change the
value of a const). This much I understand,
I'm not sure there is anything else *to* understand.
what I don't understand is
how come this is considered trying to bind a reference to a temporary,
Because the definition of "temporary object" includes the foo object
created in

bar(foo());
are not all parameters supposed to be evaluated before the function is
executed? And these evaluations should take place in the same scope as
in which the function is called? While the function, including its
parameters, are executed in its own scope?
Yes to all three, none of which affect the fact that the foo object
created by the statement bar(foo()); is a temporary object.
The way I see things the foo()-part of bar(foo()); should already have
executed (and thus have created a foo-object on the stack)
Yes. An unnamed temporary foo object now exists in the scope of main.
when the
parameter f comes into scope* and thus, from the point of view of
bar() be non-temporary.
The scope of bar isn't where you should be thinking. Assuming you
rewrite bar to take a const foo& so your code compiles, there is no
way (as far as I know) that you can tell *from inside bar* whether the
object referred to by f is a temporary or not in the scope of the
calling function. But from inside bar isn't what's important. What's
important is from inside the function that calls bar (main in this
case).

Inside main, there is a temporary foo object created and you can't
bind non-const references to temporaries. If I add a line to your main
function so you have...

int main() {
bar(foo());
foo& a_reference = foo();
}

....both statements have the same problem. They both try and bind a
temporary to an non-const reference. The fact that in the first case
the non-const reference happens to be a function parameter doesn't
matter. In both cases the object *referred to* is a temporary so the
reference has to be const.
Or put another way, I can't quite see the difference between the
following two:
bar(foo());
and
foo f;
bar(f);
Maybe you can now?

HTH
Gavin Deane

Feb 15 '07 #2
On Feb 15, 2:51 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
struct foo {
int i;
};
int bar(foo& f) {
return f.i++;
}
int main() {
bar(foo());
}
The above code does not compile since you can't bind a reference to a
temporary, you could solve this by using a 'const foo&' parameter
instead but then you have other problems (like trying to change the
value of a const). This much I understand,

I'm not sure there is anything else *to* understand.
what I don't understand is
how come this is considered trying to bind a reference to a temporary,

Because the definition of "temporary object" includes the foo object
created in

bar(foo());
are not all parameters supposed to be evaluated before the function is
executed? And these evaluations should take place in the same scope as
in which the function is called? While the function, including its
parameters, are executed in its own scope?

Yes to all three, none of which affect the fact that the foo object
created by the statement bar(foo()); is a temporary object.
The way I see things the foo()-part of bar(foo()); should already have
executed (and thus have created a foo-object on the stack)

Yes. An unnamed temporary foo object now exists in the scope of main.
when the
parameter f comes into scope* and thus, from the point of view of
bar() be non-temporary.

The scope of bar isn't where you should be thinking. Assuming you
rewrite bar to take a const foo& so your code compiles, there is no
way (as far as I know) that you can tell *from inside bar* whether the
object referred to by f is a temporary or not in the scope of the
calling function. But from inside bar isn't what's important. What's
important is from inside the function that calls bar (main in this
case).

Inside main, there is a temporary foo object created and you can't
bind non-const references to temporaries. If I add a line to your main
function so you have...

int main() {
bar(foo());
foo& a_reference = foo();

}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference. The fact that in the first case
the non-const reference happens to be a function parameter doesn't
matter. In both cases the object *referred to* is a temporary so the
reference has to be const.
Or put another way, I can't quite see the difference between the
following two:
bar(foo());
and
foo f;
bar(f);

Maybe you can now?
Sorry but no, in both cases the foo-object is temporary, but in one
there's a name and in the other there isn't. I guess I'm kind of
looking for a rationale for this behaviour, and the only thing I can
think of is that if it were allowed you would lose an opportunity to
optimize, namely the ability to create the copied parameter in place
in the stackframe of the function, whereas that would not be possible
if a reference was used.

I just can't see any advantage of the current behaviour over the one I
described nor can I see reason why it should not be possible to
implement (though, admittedly I'm no compiler developer). On the other
hand I can see some usages of allowing the behaviour I described,
among other things a number of algorithms in the standard library
could become more useful.

--
Erik Wikström

Feb 15 '07 #3

"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
>
int main() {
bar(foo());
foo& a_reference = foo();
}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference.
Also keep in mind that for both versions the copy ctor of foo has to be
accessible if the reference were to be const. And this, in fact, does work

int main()
{
foo& a_reference(foo());
}

Even without accessible copy ctor. These things strike me as odd. In what
situations a copy is needed to bind the temporary to a (const) reference?
And if [ foo& a = foo(); ] is not allowed, why is [ foo& a(foo()); ]
allowed? And why does the latter _not_ require a copy ctor?

- Sylvester
Feb 15 '07 #4
Sylvester Hesp wrote:
"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
>>
int main() {
bar(foo());
foo& a_reference = foo();
}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference.

Also keep in mind that for both versions the copy ctor of foo has to
be accessible if the reference were to be const. And this, in fact,
does work
int main()
{
foo& a_reference(foo());
It's a declaration of a function. Of course it "does work".
}

Even without accessible copy ctor. These things strike me as odd. In
what situations a copy is needed to bind the temporary to a (const)
reference? And if [ foo& a = foo(); ] is not allowed, why is [ foo&
a(foo()); ] allowed? And why does the latter _not_ require a copy
ctor?
- Sylvester
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #5
On 15 Feb, 15:41, "Sylvester Hesp" <s.h...@oisyn.nlwrote:
int main()
{
foo& a_reference(foo());

}
That's a function declaration.

Gavin Deane

Feb 15 '07 #6

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:er**********@news.datemas.de...
Sylvester Hesp wrote:
>"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@s48g2000cws.googleg roups.com...
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
>>>
int main() {
bar(foo());
foo& a_reference = foo();
}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference.

Also keep in mind that for both versions the copy ctor of foo has to
be accessible if the reference were to be const. And this, in fact,
does work
int main()
{
foo& a_reference(foo());

It's a declaration of a function. Of course it "does work".
d'Oh!
I feel so stupid right now.

Nevertheless, why the need for accessible copy ctors?

- Sylvester
Feb 15 '07 #7
On 15 Feb, 15:30, "Erik Wikström" <eri...@student.chalmers.sewrote:
On Feb 15, 2:51 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.sewrote:
struct foo {
int i;
};
int bar(foo& f) {
return f.i++;
}
int main() {
bar(foo());
}
<snip>
Or put another way, I can't quite see the difference between the
following two:
bar(foo());
and
foo f;
bar(f);
Maybe you can now?

Sorry but no, in both cases the foo-object is temporary, but in one
there's a name and in the other there isn't.
Depends what you mean by "temporary". In a way, all variables with
automatic storage duration are "temporary" in that they don't last
forever. I had hoped to find a formal definition of "temporary" in the
standard, but if it's there it eluded me. But when I used the word
temporary in my post, i meant "temporary" as used by the wording of
the C++ standard. And by that (less precise than I would have liked)
definition, the foo object in

bar(foo());

is a temporary, while the foo object in

foo f;
bar(f);

is not a temporary.
I guess I'm kind of
looking for a rationale for this behaviour, and the only thing I can
think of is that if it were allowed you would lose an opportunity to
optimize, namely the ability to create the copied parameter in place
in the stackframe of the function, whereas that would not be possible
if a reference was used.

I just can't see any advantage of the current behaviour over the one I
described nor can I see reason why it should not be possible to
implement (though, admittedly I'm no compiler developer). On the other
hand I can see some usages of allowing the behaviour I described,
among other things a number of algorithms in the standard library
could become more useful.
Rationale is different. I was attempting to explain how the foo object
in bar(foo()); is temporary and so cannot be bound to a non-const
reference, assuming the definitions of all those terms as used in the
standard. As to *why* things are that way, that's a different question
(and one I'm not sure of - I'm not a compiler developer either). Maybe
someone else here can shed some light. Or perhaps comp.std.c++?

Gavin Deane

Feb 15 '07 #8
Sylvester Hesp wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:er**********@news.datemas.de...
>Sylvester Hesp wrote:
>>"Gavin Deane" <de*********@hotmail.comwrote in message
news:11**********************@s48g2000cws.google groups.com...
On 15 Feb, 13:16, "Erik Wikström" <eri...@student.chalmers.se>
wrote:

int main() {
bar(foo());
foo& a_reference = foo();
}

...both statements have the same problem. They both try and bind a
temporary to an non-const reference.

Also keep in mind that for both versions the copy ctor of foo has to
be accessible if the reference were to be const. And this, in fact,
does work
int main()
{
foo& a_reference(foo());

It's a declaration of a function. Of course it "does work".

d'Oh!
I feel so stupid right now.

Nevertheless, why the need for accessible copy ctors?
Because during binding to a reference a copy may need to be made.
It is needed if conversion happens, as in

void foo(const int&);
foo(3.1415926); // the temporary 'int' is created and bound
// to the reference

As to why the need to make a copy... I am not sure. Try looking
in the archives. This undoubtedly has been discussed before. Just
search for "accessible copy constructor bind reference" (without
the quotes).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 15 '07 #9
Erik Wikström <er****@student.chalmers.sewrote:
Sorry but no, in both cases the foo-object is temporary, but in one
there's a name and in the other there isn't. I guess I'm kind of
looking for a rationale for this behaviour, and the only thing I can
think of is that if it were allowed you would lose an opportunity to
optimize, namely the ability to create the copied parameter in place
in the stackframe of the function, whereas that would not be possible
if a reference was used.
From what I understand, the rationale is that if a conversion is
required, then the behavior can be surprising. For example, suppose
that it were possible to bind a temporary to a non-const reference:

void foo(int& i)
{
++i;
}

void bar()
{
foo(int(3));
foo(double(3.0));
}

OK, so this example is contrived, but stick with me. When we call foo()
with the temporary int, things are as expected and foo increments the
actual int. However, when we call foo() with the double, it must first
create another temporary int, and then it passes this temporary int to
foo(). Then, foo() will increment the temporary int and not the
original double.

This example seems pretty pointless, but imagine instead if int& were
replaced with some class type, and ++i were replaced with a call to some
non-const member function of that class.

Therefore it would appear that foo() works in some cases but not in
others. In order to reconcile this difference, they decided not to
allow binding temporaries to non-const references.
I just can't see any advantage of the current behaviour over the one I
described nor can I see reason why it should not be possible to
implement (though, admittedly I'm no compiler developer). On the other
hand I can see some usages of allowing the behaviour I described,
among other things a number of algorithms in the standard library
could become more useful.
AFAIK there is no technical reason why it cannot be done, and in fact
some compilers (e.g., recent versions of Visual Studio) actually allow
you to bind a temporary to a non-const reference, as an extension.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 16 '07 #10
Erik Wikstrom wrote:
>
>Sorry but no, in both cases the foo-object is temporary, but in one
there's a name and in the other there isn't. I guess I'm kind of
looking for a rationale for this behaviour, and the only thing I can
think of is that if it were allowed you would lose an opportunity to
optimize, namely the ability to create the copied parameter in place
in the stackframe of the function, whereas that would not be possible
if a reference was used.
I do not know wat is rationale. Some compilers can compile that and will
produce warnings if temporary will be created. I have read sometimes ago
something somewhere about, but already forget why.

Sylvester Hesp wrote:
>
In what situations a copy is needed to bind the temporary to a (const)
reference? And if [ foo& a = foo(); ] is not allowed, why is [ foo&
a(foo()); ] allowed? And why does the latter _not_ require a copy ctor?
First [ foo& a = foo(); ] does _not_ require a copy ctor also.

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 21 '07 #11
Grizlyk wrote:
[..]
Sylvester Hesp wrote:
>>
In what situations a copy is needed to bind the temporary to a
(const) reference? And if [ foo& a = foo(); ] is not allowed, why is
[ foo& a(foo()); ] allowed? And why does the latter _not_ require a
copy ctor?

First [ foo& a = foo(); ] does _not_ require a copy ctor also.
foo& a = foo();

does *not* compile with a fully compliant compiler.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 21 '07 #12

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

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
6
by: Matthias Kaeppler | last post by:
Hello, during a discussion on a C++ internet forum, some question came up regarding references and the lifetime of the objects they alias. I can't find any clear wording on that in the draft...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
1
by: Neelesh Bodas | last post by:
Hello all, Please consider this code : class X { int x; public: X(int p ) : x(p) { } operator int() { x = 1; return x; } };
7
by: REH | last post by:
Though unsafe, is this legal: const int& foo(const int& i) { return i; } int j = foo(5); does the temporary live long enough to be assigned to j?
28
by: Frederick Gotham | last post by:
When I was a beginner in C++, I struggled with the idea of references. Having learned how to use pointers first, I was hesitant to accept that references just "do their job" and that's it. Just...
12
by: dave_dp | last post by:
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...
5
by: VSP | last post by:
Hi, I have a doubt regarding using references. Please look at the below code. I am using VC++ 6.0 int &i = 10; // Compilation error error C2440: 'initializing' : cannot convert from...
6
by: wizwx | last post by:
Is there anything wrong with the following code? class A { ... }; class B : public A { ... }; // definitions of class A and B, these are OK Foo() { A & a = B(); // ?? A * p = &B(); // ??...
2
by: casul | last post by:
Hi All, Given the following code : char const & identity( char const &c ) { return c; } char const & f( ) { return identity('A'); } char const & g( ) { return 'A'; } Of course, the...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.