any news on const/invariant?
Bill Baxter
dnewsgroup at billbaxter.com
Mon Dec 17 15:50:53 PST 2007
Oskar Linde wrote:
> 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.
If the compiler can verify that no attempts were made to modify the
struct when you label it "const" then it can also detect that no
attempts were made to modify it even if you don't label it "const". And
if so, it can do whatever optimization it would have done given a
"const" label without burdening the developer with thinking about it.
--bb
More information about the Digitalmars-d
mailing list