reference types

D

Daniel Oberhoff

Hi,

Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.
 
W

WaterWalk

Hi,

Can someone more proficient in the details of the c++ type system
please tell me how I can instantiate the for constesses of references
that I think should be possible:

1. non-constant reference to non-constant value
2. constant reference to non-constant value
3. non-constant reference to constant value
4. constant reference to constant value

const T & seems to behave like the latter, and I could not find any
good reference on this. I would like to be able to use references to
non-const rvalues.

Not like a pointer, a reference must refer to something. If the object
is const, the reference must be const qualified. But if the object
isn't const, the reference can be either const or non-const qualified.
 
D

Daniel Oberhoff

There is no such thing as a "non-constant" reference, so 1 and 3 above
are impossible.

2. "T&"
4. "const T&"

If you think you need a non-constant reference, use a pointer.

ah, ha! I always thought you could change the referee of a reference,
thus the reference could be non-const referring to several const
referees in its lifetime (this is a strange use of the word referee as
"reffered-to-object", but it feels like proper use of english
construction rules...is it?). but actually reading another post and
thinking about it I realized it is not, since the reference inherits
the operator= from the referee. I suppose I got used to the idea since
I deal with blitz arrays a lot, which use refcounted storage and the
target storage can be changed, which is like changing the referee, but
explicitly using a function call. ah well :). Though while at it: I
know in c++ it is not allowed to (non-const) reference an rvalue,
though there are times when it makes sense to modify and rvalue, while
not wanting to copy it. Though I suppose this would have to involve
refcounting...

Daniel
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top