473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Referenece binding to const temporaries

Hello all,

I've been wondering... Why is it that a reference may be bound only to a
const object? If a reference were bound to a non-const object and that
object were modified, what harm could result? A temporary is just as real
of an object as any other. It lacks a name, but that doesn't make it less
real. Class (no pun intended) warfare seems to be alive and well!

Thanks,
Dave
Jul 22 '05 #1
5 1514
Dave wrote:
Hello all,

I've been wondering... Why is it that a reference may be bound only to a
const object? If a reference were bound to a non-const object and that
object were modified, what harm could result? A temporary is just as real
of an object as any other. It lacks a name, but that doesn't make it less
real. Class (no pun intended) warfare seems to be alive and well!

Thanks,
Dave


Hm? What keeps you from doing this:

class A {};

A a;
A& ref = a;

// now work on ref (and therefore on a)

No const here.
Jul 22 '05 #2

"matthias_k" <no****@digitalraid.com> wrote in message
news:ct*************@news.t-online.com...
Dave wrote:
Hello all,

I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that
object were modified, what harm could result? A temporary is just as real of an object as any other. It lacks a name, but that doesn't make it less real. Class (no pun intended) warfare seems to be alive and well!

Thanks,
Dave


Hm? What keeps you from doing this:

class A {};

A a;
A& ref = a;

// now work on ref (and therefore on a)

No const here.


Sorry, my original post missed a key word - temporary. Why may only a
*const* reference be bound to a *temporary* object?
Put differently, what is the rationale for the rule that says I may not bind
a non-const reference to a temoirary object?
Jul 22 '05 #3
Dave wrote:
"matthias_k" <no****@digitalraid.com> wrote in message
news:ct*************@news.t-online.com...
Dave wrote:
Hello all,

I've been wondering... Why is it that a reference may be bound only to
a
const object? If a reference were bound to a non-const object and that
object were modified, what harm could result? A temporary is just as
real
of an object as any other. It lacks a name, but that doesn't make it
less
real. Class (no pun intended) warfare seems to be alive and well!

Thanks,
Dave


Hm? What keeps you from doing this:

class A {};

A a;
A& ref = a;

// now work on ref (and therefore on a)

No const here.

Sorry, my original post missed a key word - temporary. Why may only a
*const* reference be bound to a *temporary* object?
Put differently, what is the rationale for the rule that says I may not bind
a non-const reference to a temoirary object?


If you could hold references to a (soon to be deleted) temporary, you
would be able to change the temporary through the alias name, although
this temporary may already be deleted. I guess this would be a situation
of undefined behavior.
Only allowing const references to reference a temporary keeps you from
doing such things.

Regards,
Matthias
Jul 22 '05 #4
> > Sorry, my original post missed a key word - temporary. Why may only a
*const* reference be bound to a *temporary* object?
Put differently, what is the rationale for the rule that says I may not bind a non-const reference to a temoirary object?


If you could hold references to a (soon to be deleted) temporary, you
would be able to change the temporary through the alias name, although
this temporary may already be deleted. I guess this would be a situation
of undefined behavior.
Only allowing const references to reference a temporary keeps you from
doing such things.

Regards,
Matthias


However, the temporary persists until all references to it are gone. This
is already the case with a const reference being bound to a temporary.
Jul 22 '05 #5
In article <10*************@news.supernews.com>,
"Dave" <be***********@yahoo.com> wrote:
Sorry, my original post missed a key word - temporary. Why may only a
*const* reference be bound to a *temporary* object?
Put differently, what is the rationale for the rule that says I may not bind a non-const reference to a temoirary object?


If you could hold references to a (soon to be deleted) temporary, you
would be able to change the temporary through the alias name, although
this temporary may already be deleted. I guess this would be a situation
of undefined behavior.
Only allowing const references to reference a temporary keeps you from
doing such things.

Regards,
Matthias


However, the temporary persists until all references to it are gone. This
is already the case with a const reference being bound to a temporary.


The motivation is spelled out in Stoustrup's excellent "The Design and
Evolution of C++", section 3.7. There's a good discussion, but it boils
down to this example:

void incr(int& rr) {rr++;}

void g()
{
double ss = 1;
incr(ss);
}

If temporaries could bind to non-const references then this program
would compile, but ss would not get incremented. By enforcing the
existing rule you turn a run time error into a compile time error (which
is always a Very Good Thing). You should really get the book for the
full story.

However, there is a move afoot to introduce a new reference type which
/will/ bind to temporaries. This characteristic, when combined with the
existing reference, can yield some extremely useful functionality.

http://www.open-std.org/jtc1/sc22/wg...004/n1690.html

-Howard
Jul 22 '05 #6

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

Similar topics

1
by: kaede | last post by:
Hi all, I would like to know if the following code is valid and not ill-formed. Data getData() { return Data(1, 2, 3); }
3
by: kaede | last post by:
Hi all, Consider the following code fragment: // some data structure class Data { ... } // Container for the data structure Class Container {
39
by: JKop | last post by:
Back when I read my first C++ book, I was given the following scenario: class Cheese { public: int number_of_holes; int colour;
10
by: JKop | last post by:
Firstly, we all know that temporaries are non-const, which I shall demonstrate with the following code: struct Blah { int monkey; void SetMonkey(int const supplied) { monkey = supplied;
8
by: bipod.rafique | last post by:
Hello All, I need your help in understanding something. I have a simple class class test{ };
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...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
7
by: siddhu | last post by:
Dear Experts, I have a question. I wrote the following code class A { public: void g(){}
14
by: Javier | last post by:
Hello, in which cases is it better the use of "const char*" to "string" (or even const string &). I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see: hash_map<const char*, int,...
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
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,...
1
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...
0
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.