Boost logo

Boost :

From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2002-08-01 04:39:26

Mattias Flodin wrote:
> I had a quick look at the filesystem library headers and noticed this
> comment in path.hpp:
> // Rationale for returns of "const path" instead of "path"; see
> // Scott Meyers, EC++, Item 21.
> Now, that item says that operator+() and others returning a const object
> 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, should
> 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.
Dave, what is the current status of the proposal for the
operators.hpp-changes? Are you still waiting for Aleksey's and Daryle's
agreement? I asked both Jeremy and John about the Borland fixes, but
Jeremy had no time (and wasn't the originator of the Borland fixes) and
John (who provided the fixes) wasn't interested in maintaining them. He
thinks (and I agree with him), that the changes are OK and we should
wait if some Borland users complain. My part should be a better
documentation, but I didn't considered it worth another post (I have it
at home, so I can provide it on request tomorrow evening if you like).

> Secondly, if it is really a good idea to return const objects for all
> functions and not just operators, should that be a coding policy for
> boost? Should we look over the other libraries and make sure they all
> return const objects?

IMHO returning const objects where possible is a good idea, except there
is a definite use case for non-const objects. I use this style in the
companies code and it seems to have no drawbacks, but it prevents stupid
typos like this one to compile:

// Signatures:
class A;
A f( int x, int y );
A a;

// The problem:
if( f( x, y ) = a ) { ... } // Should have been ==, but compiles without
errors :(

with 'const A f( int x, int y );', you get a compile-error. :)

Regards, Daniel

Daniel Frey
aixigo AG - financial training, research and technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey_at_[hidden], web:

Boost list run by bdawes at, gregod at, cpdaniel at, john at