A New Krazy Checker for Methods Returning const refs

Thiago Macieira thiago at kde.org
Sun Apr 15 02:07:52 BST 2007


Jos van den Oever wrote:
>returning X instead of const X& should only be preferred if it is
>cheap to copy the class. This is the case for QList, but not
>(necessarily) for std::list. I'd even say that for most classes,
>returning const X& is to be preferred over returning X. Checking
>against a list of classes that are known to be implicitly shared.

You're right, but I'd say your solution isn't the best possible. Instead 
of returning const-refs of expensive-copy types, code should be made to 
use implicit- or cheap-copy types.

Returning a const-ref is inherently thread unsafe and also imposes strict 
constraints on the class's internals. You need to have a variable of that 
type somewhere and you simply cannot change it while the caller is using 
the returned value.

No, the test is correct. Any such exceptional cases of const-ref returns 
should be marked by explicit Krazy test disables.

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    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: 189 bytes
Desc: not available
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070415/237f1d62/attachment.sig>


More information about the kde-core-devel mailing list