[Exceptional C++ Style] Exceptional C++ Style - Item 24: Cons tantOptimization

David Nash David.Nash at WallStreetSystems.com
Mon Jan 24 12:49:59 EST 2005


Balog Pal wrote:
 
> The really short summary of the item: 'const' applied to a 
> reference is just 
> a programmer-related local promise and the compiler can't use it for 
> anything, optimisation included.  And the purpose of const is 
> completely 
> different.

Yes. I was originally going to make a comment like: "const tells
the function implementor (and the caller) that the function is not 
supposed to change the object.  Nothing more, really.

> Those are completely unrelated issues.  const in the function 
> param list 
> means "this function supposed not and will not alter the 
> string passed". 
> What the caller drops in is the caller's stuff.

True, but not unrelated - because it's the "caller's stuff", the
compiler of a function cannot assume that the object passed is truly const.
This is the point Herb was trying to make.


> An old guideline said const  (non-ref) params belong to the 
> *impelmentation 
> details*. So in headers and function interfaces const shall 
> be omitted (it's 
> discarded by the compiler anyway). 

True but it can be useful documentation for the caller.

> And the implementation can 
> insert cont at 
> will if it feels like it.

This is where it is more useful (to the implementor), IMHO.

Dave




More information about the Effective-cpp mailing list