473,769 Members | 2,081 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
11 1604
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
3080
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
1509
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
1539
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
1410
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
2039
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
2594
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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,...
1
9997
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
9865
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...
1
7413
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.