any news on const/invariant?

Oskar Linde oskar.lindeREM at OVEgmail.com
Mon Dec 17 11:04:59 PST 2007


Bill Baxter wrote:

> I can't speak for all the cases the original poster was talking about, 
> but const on value parameters is useless excess verbiage in my opinion.
> 
> float sin(const float) { ... }
> 
> Here I'm recording my design decision that my implementation of sin will 
> not change its copy of the float argument passed in.
> 
> Great.  Callers just don't care.  Such uses of const clutter up the 
> interface for no good reason in my opinion.  Most other uses of const on 
> simple value types I'm ok with.

Isn't there a value in knowing the argument will never change when 
passing potentially large structs to functions by value?

For example, calling

void foo(Struct a);

will in most cases mean that a copy of the passed Struct is made on the 
callers stack, and the address to that copy is then passed to the 
callee. If the compiler knew the Struct argument never changed, it could 
avoid making several copies for consecutive calls to such functions, and 
if the called function was pure, avoid making a copy at all.

-- 
Oskar



More information about the Digitalmars-d mailing list