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