const

Janice Caron caron800 at googlemail.com
Fri Mar 28 12:17:30 PDT 2008


On 28/03/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
>  > const int x = 5;
>  >
>  > D 2.010 assigns the type of &x const(int)*. But I would consider that
>  > wrong. The type should be invariant(int)*,
>
> You're right, but again, the problem comes up when you're writing
>  templates that deconstruct and construct types. Taking these semantic
>  shortcuts in the compiler cause havoc with templates. That's why we
>  finally gave up on such and went to a consistent, pedantic approach.

That's true, but there's a new idea on the block now. A few posts up,
I suggested that a declaration like

    const int x = 5;

should be interpretted by the compiler as if the statement had been

    invariant int x = 5;

(Actually, I used the nonclamenture in/const, instead of
const/invariant, but the meaning doesn't change with the words).

Essentially, I propose the rule that if any variable is declared to be
a fully const POD type, then it should be magically retyped to instead
become fully invariant. Thus, typeof(x) would be invariant(int),
typeof(&x) would be invariant(int)*, and so on. Hopefully, you can see
that deconstructing and reconstructing would now still work perfectly.



More information about the Digitalmars-d mailing list