470,855 Members | 1,322 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

reference types

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.

Jun 27 '08 #1
5 1529
Daniel Oberhoff <da*****@phys.ethz.chwrote:
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.
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.
Jun 27 '08 #2
On May 17, 11:17 pm, Daniel Oberhoff <dani...@phys.ethz.chwrote:
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.
Jun 27 '08 #3
On 2008-05-17 17:41:35 +0200, "Daniel T." <da******@earthlink.netsaid:
Daniel Oberhoff <da*****@phys.ethz.chwrote:
>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.

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

Jun 27 '08 #4
Daniel Oberhoff <da*****@phys.ethz.chwrote:
"Daniel T." <da******@earthlink.netsaid:
Daniel Oberhoff <da*****@phys.ethz.chwrote:
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.
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?).
No. The word you are looking for is "referent". I don't want to sound
too authoritative on this subject though, my wife happens to be an
English major and I asked her. :-)
but actually reading another post and thinking about it I realized
it is not, since the reference inherits the operator= from the
referee.
I understand what you are trying to say here, but strictly technically
what you said is incorrect. The reference doesn't *inherit* the op= from
the referent. It's better to think of the reference as simply another
name for the the referent, an alias, rather than something that is
somehow separate from the referent.
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 :).
Sounds like a ref-counted smart pointer. Boost has a good implementation
of it if you want to use one. It's also a handy thing for a beginner to
write if he wants to learn more about operator overloading.
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.
I'm not so sure I agree with the sentiment...
Jun 27 '08 #5
Daniel Oberhoff <da*****@phys.ethz.chwrote in news:4831eade$0$6510
$9*******@newsspool4.arcor-online.net:
[...]
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...
Not at all. Calling a non-const member function on a temporary is legal,
and this non-const member function can return a lvalue reference. So one
can turn a rvalue into a lvalue easily, the intent has just be expressed
explicitly:

// pseudocode:
enum MsgType {...}; // message type
class Data {...}; // a "variant" data type.

class GuiMessage {
public:
GuiMessage(MsgType type, Data arg1, Data arg2);
GuiMessage& Ref() {return *this;}
void Swap(GuiMessage& other); // swaps the contents
...
};

void PostToQueue(GuiMessage& message) {
...
message.Swap(to_queue_internals);
...
}
PostToQueue( GuiMessage( msgtype_update_all_views, this, NULL).Ref());
Here, GuiMessage objects are constructed only for posting them into some
message queue. There is no point for keeping the object alive at the call
site so they are constructed as temporaries. For gaining non-const access
for swapping the contents away the Ref() function is used.

C++0x will provide moving constructors and rvalue references, which can
be used to accomplish the same more systematically.

Regards
Paavo
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Nader | last post: by
5 posts views Thread by Javier Campos | last post: by
4 posts views Thread by Edward Diener | last post: by
12 posts views Thread by Andrew Bullock | last post: by
9 posts views Thread by Edward Diener | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.