Referenece binding to const temporaries

D

Dave

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
 
M

matthias_k

Dave said:
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.
 
D

Dave

matthias_k said:
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?
 
M

matthias_k

Dave said:
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
 
D

Dave

Sorry, my original post missed a key word - temporary. Why may only a
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.
 
H

Howard Hinnant

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.[/QUOTE]

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/wg21/docs/papers/2004/n1690.html

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,336
Members
48,014
Latest member
saradhi

Latest Threads

Top