const a storage class or a type modifier?

Jens Mueller jens.k.mueller at gmx.de
Fri Nov 26 06:12:53 PST 2010


Michel Fortin wrote:
> On 2010-11-26 07:51:20 -0500, Jens Mueller <jens.k.mueller at gmx.de> said:
> 
> >Trass3r wrote:
> >>>In TDPL const/immutable are type qualifiers. Type qualifier is a
> >>>synonym for type modifier, isn't it? And I know storage classes like
> >>>static/ref. In C you have the type qualifiers const, volatile, and
> >>>restrict and the storage classes auto, register, static, extern. Now
> >>>const in D is a storage class?
> >>
> >>I also think const char* x in D2 is equal to const(char*) x while a
> >>direct translation would be const(char)* x
> >>So you might use the former version to keep compatibility with D1.
> >
> >Haven't thought about that.
> >Right. In D2 const T* is equal to const(T*).
> >
> >Let's check all the possible cases:
> >void foo(const int *non_const_ptr_to_const,
> >         char *const const_ptr_to_non_const,
> >         const float *const const_ptr_to_const);
> >
> >In D2 you cannot express char *const. That's due to the transitivity of
> >const. You can only do a const char* which is a constant pointer to
> >constant.
> >That's why I think foo should become in D2
> >void foo(const int*, char *, const(float*));
> >
> >What do you think?
> 
> const(float*) or const(float)* are pretty much equivalent as a
> function parameter type. That's because the pointer is being passed
> to the function by copy. So whether it is const or not only matter
> when you are writing the function's body (can't mutate the local
> variable). It doesn't change anything for the caller.

That's true.
So how do you like it to be?
void foo(const(int)*, char *, const(float)*);
or
void foo(const int*, char *, const float*);

I still like the fixed original one namely
void foo(const(int)*, char *, const float*);

Because it's the most explicit one and one should know the difference.
Even though one is not implementing the function. But the signature
tells you something. If you do const float* it tells you that the
implementation isn't changing its copied pointer. If you do
const(float)* then it may change its copied value of the pointer.
But I agree it's rather artificial because it's a copy anyway. But I
like it to be explicit. In general I prefer the const() notation more
than the one without round brackets when dealing with types.

Jens


More information about the Digitalmars-d mailing list