473,757 Members | 10,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1601
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
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...@ho tmail.comwrote:
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
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*********@ho tmail.comwrote in message
news:11******** **************@ s48g2000cws.goo glegroups.com.. .
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
>
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*********@ho tmail.comwrote in message
news:11******** **************@ s48g2000cws.goo glegroups.com.. .
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
>>
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.n lwrote:
int main()
{
foo& a_reference(foo ());

}
That's a function declaration.

Gavin Deane

Feb 15 '07 #6

"Victor Bazarov" <v.********@com Acast.netwrote in message
news:er******** **@news.datemas .de...
Sylvester Hesp wrote:
>"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11******* *************** @s48g2000cws.go oglegroups.com. ..
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
>>>
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.sewro te:
On Feb 15, 2:51 pm, "Gavin Deane" <deane_ga...@ho tmail.comwrote:
On 15 Feb, 13:16, "Erik Wikström" <eri...@student .chalmers.sewro te:
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.********@com Acast.netwrote in message
news:er******** **@news.datemas .de...
>Sylvester Hesp wrote:
>>"Gavin Deane" <de*********@ho tmail.comwrote in message
news:11****** *************** *@s48g2000cws.g ooglegroups.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.sewro te:
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

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

Similar topics

17
3078
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 pointers for my references to objects. There are 3 reasons why I find them inadequate: 1 - Can't be null. 2 - Can't be reseated. Now I'm sure there are good reasons for these first 2, but it's #3 that I
6
1508
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 standard. Example 12.2 in the standard document illustrates that temporaries bound to references-to-const live as long as the reference does. But does it e.g. matter if the temporary was created in scope of a function body and has to outlive the...
10
1538
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 at least flag this as a warning as they would when say trying to return a const & to a local? In Section #2, the const B& Bref is initialized and bound to the temporary returned from GetSettings(). That is the temporary B exists until Bref goes...
1
1409
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
1692
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
2038
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 recently, a poster posted looking for an explanation of references. I'll give my own understanding if it's worth anything. First of all, the C++ Standard is a very flexible thing. It gives a mountain of freedom to implementations to do things...
12
2593
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 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...
5
1694
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 'const int' to 'int &' A reference that is not to 'const' cannot be bound to a non-lvalue
6
1555
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
2364
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 compiler complains of function 'g' returning a
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.