Const sucks
Sean Kelly
sean at f4.ca
Mon Sep 10 16:12:33 PDT 2007
Walter Bright wrote:
> Sean Kelly wrote:
>
>>> o tail const of a struct would have to be done by making the struct
>>> a template:
>>>
>>> struct S(T) { T member; }
>>> S!(int) // tail mutable
>>> S!(const(int)) // tail const
>>>
>>> o one can construct a template to generically produce tail const or
>>> tail invariant versions of a type.
>>
>> Could you please explain this further? Why would templates be needed
>> in the above two points?
>
> Think of a struct which was your own implementation of arrays:
>
> struct Array(T)
> {
> T[] a;
> }
>
> To create a tail const array, instead of const(T)[], we'd do
> Array!(const(T)).
Okay, so basically if I want a mutable struct that may reference
constant data I'll have to pass the const attribute through via a
template rather than preceding the struct decl with a "tail const"
keyword. Is that right? Seems fair enough. C++ requires the same
approach, as far as I know.
Sean
More information about the Digitalmars-d
mailing list