Boost logo

Boost :

From: Bill Klein (bill_at_[hidden])
Date: 1999-12-29 14:21:05


Steve wrote:
>I am completely convinced by your example. There are no good
>alternatives that I can think of.
>
>However, I would like to change it to return a constant object.
>This would prevent code such as:
>
> // x is an auto object
> // k is a public integer data member on the function object
> x.action().k = 13;
>
>which may lead the user to believe that he has changed the value
>of x's function object, when in fact that line of code does
>nothing. Returning a constant object will not allow this code
>to compile.
>
>Are there any drawbacks to returning a const object instead of
>an object?

I don't know of any, and I've seen this recommended before,
but wouldn't any compiler (that's worth using) warn you
about the line you propose ("attempt to modify a temporary")?

-Bill Klein <bill_at_[hidden]>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk