A New Krazy Checker for Methods Returning const refs

Mirko Boehm mirko at kde.org
Mon Apr 16 09:20:58 BST 2007


It is a Qt-ism that return values are supposed to be thread safe. It is 
declaredly not a policy for C++. It is, in fact, the other way around: The 
standard library classes are not syncronized, it is left to the classes that 
use them to take care of that. 

Whether a method returns a copy or a const reference is not only a matter of 
the cost of the copying, but also of the implementation of the class itself. 
For example, if a class holds a cached value in a member variable, even the 
cheap refcounted copy operation is a waste of time. 

I oppose this policy. It makes writing classes harder, since we have to 
continuously distinguish between standard library classes and our own. And I 
have a hard time seeing the benefits. 

Best regards,
--Mirko.

On Sunday 15 April 2007 03:07, Thiago Macieira wrote:
> 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.

-- 
KDE e.V.: http://ev.kde.org
-------------- 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/20070416/5c703497/attachment.sig>


More information about the kde-core-devel mailing list