Const sucks

Regan Heath regan at netmail.co.nz
Wed Sep 12 01:41:16 PDT 2007


Derek Parnell wrote:
> On Tue, 11 Sep 2007 10:10:33 +0100, Janice Caron wrote:
> 
>> On 9/11/07, Derek Parnell <derek at nomail.afraid.org> wrote:
>>> In other words, if I have a struct with three members, each of a different
>>> type, I need to code ...
>>>
>>> struct S3(T, U, V)
>>> {
>>>    T member1;
>>>    U member2;
>>>    V member3;
>>> }
>>>
>>> S3!(const(int), const(float), const(bool));
>>>
>>> and so on for 4, 5, 6, .... 23 member structs.
>>>
>>> I'm sure I'm misunderstanding you, because this is really silly.
>>>
>> I don't think you're misunderstanding. I think that's what Walter is saying.
> 
> But why templates?!?!?
> 
> How is that different from ...
> 
>  struct S3
>  {
>     const(int) member1;
>     const(float) member2;
>     const(bool) member3;
>  }
> 
> No template involved.
> 
> I'm reading Walter as saying that if any struct definition involves any of
> its members being const/invariant, then one must define that struct in
> terms of templates. Is this what Walter is really saying?

No, I don't think so.

I believe he is saying that if you have a struct, eg.

struct S1
{
   int a;
   float b;
}

and you want to use it, as is, in one place in your code but also use a 
const version of this same struct somewhere else then you need to use a 
template to create a modified struct definition to use.

If you simply want to convert a struct from non-const aware to 
const-aware you can just add const to the definition and recompile.

If you simply want to have a const copy of the struct somewhere you can 
just use const(S1), eg.

class Bob
{
   const(S1) data;
}

Regan



More information about the Digitalmars-d mailing list