From: Daniel Frey (d.frey_at_[hidden])
Date: 2002-08-01 17:11:14
On Thu, 01 Aug 2002 17:58:09 +0200, David Abrahams wrote:
> ----- Original Message -----
> From: "Daniel Frey" <daniel.frey_at_[hidden]>
>> > Now, that item says that operator+() and others returning a const
>> > is "The Right Thing To Do" because it prevents you from doing
>> > silliness such as
>> > (a + b) = c
>> > which is not allowed on native types and therefore, by extension,
>> > not be allowed on user-defined types.
>> This is one of the extensions I suggested for operators.hpp. I thought
>> that discussing general programming guide-lines would lead to the
>> authors of the libraries to adapt them. Seems I'm wrong about this.
> I have mixed feelings about the above restriction. I've never written
> silliness like the above by mistake, and I can think of cases using
> expression templates where it might even be a sensible thing to write.
It is only one example of "stupid" code. The best example is still i++++;
and examples, where the user might not be aware of the *copy* of an object
that the function returns. In practice, bugs have subtle sources and the
observable symptoms are deferred. Applying methods to a copy of an object
instead of the object you was expecting is IMHO a dangerous source of
these kind of bugs.
For the chaining, you usually have one function that creates an object,
returning a non-const T and functions that change the object's state and
thus returns a T& (return *this;). Sure you need to think about when to
add 'const', but my experience is, that if you can't see any obvious
reason for a non-const object, it's better to return a const object.
Additionally I consider it documentation of what I (as the author) was
thinking when writing a function. Ommitting the const is therefore also
documentation. This is of course a somewhat radical approach that might
not be your preference :)
>> Dave, what is the current status of the proposal for the
>> operators.hpp-changes? Are you still waiting for Aleksey's and Daryle's
> Yep. I've been incredibly busy, and although I looked over the changes
> quickly I didn't hear enough definitive yay/nay feedback from other
> concerned parties to make it worth incorporating them at this stage.
> Also, I think you hadn't completed the docs.
I'll send the new docs tomorrow if my time allows it...
> Windows that would include Borland). In theory I'm on vacation until
> 8/11, so you might have to get someone else to apply the patches.
I work with Linux, thus I can only test the GCC (2.95.3 and 3.1 are
currently installed, I'll try to install the 3.1.1 later) and the Intel
C++ 6. The time is not really a problem for me, I was just wondering what
I should do next. (OK, it's about the docs :)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk