[Qt-interest] Qt code compiles fine on mscv9.0 but failed with gcc

Thiago Macieira thiago at kde.org
Wed Jan 26 22:08:44 CET 2011


On Wednesday, 26 de January de 2011 22:58:40 Constantin Makshin wrote:
> I admit it's safer to forbid such usage to make code less error-prone, but
> at the same time I think it's not as bad as you say. Correct me if I'm
> wrong.

Like I said, it breaks the concept, which is why it's not allowed. You can 
only bind a non-const reference to an object that exists and is persistent, 
otherwise the reference might outlive the object being referenced, like my 
example.

Your example is actually very bad. What happens if you don't pass a temporary, 
but a normal object? The contents are suddenly changed.

With C++0x and rvalues ref, you can pass a non-const rvalue ref and call any 
rvalue-ref methods on it, which may modify the object (after all, they're not 
const).

But with lvalue refs, this is wrong. You cannot bind a temporary to an lvalue 
ref since the temporary is, by definition, not an lvalue.

The most evident case of taking a non-const rvalue ref is the "move 
constructor":

Foo::Foo(Foo &&other) 
    : d(other.d)
{
    other.d = 0;
}

As you can see, it does modify the parameter passed. But it does that 
*because* it knows that the object is a temporary and is getting thrown away 
soon.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110126/8c78aaa8/attachment.bin 


More information about the Qt-interest-old mailing list