const attribute makes whole element const?

monarch_dodra monarchdodra at gmail.com
Sun Sep 9 05:52:50 PDT 2012


On Saturday, 8 September 2012 at 23:18:14 UTC, Timon Gehr wrote:
> On 09/09/2012 01:16 AM, Namespace wrote:
>> Why fail this code?
>> without "const" on "Name" it works fine.
>>
>> http://dpaste.dzfl.pl/9fa0986a
>
> const fields cannot be written to. This includes the case when 
> the
> entire struct is written to at once.

That doesn't make sense to me either. Why would the entire 
"tests[4]" be immutable? If I were to define CC and opAssign, 
then why is there still a problem?

Further more, changing from Associative Array to raw array solves 
the problem.

This is a strange behavior for AAs.

void main() {
     struct Test {
         const string Name;

         this(string name) {
             this.Name = name;
         }

         this(Test other)
         {}

         void opAssign(Test other)
         {}
     }

     Test[string] tests;
     Test t2 = Test("FooBar");
     Test t3;
     t3 = t2;
     auto t4 = tests[4];
     tests[4] = t4;
}



More information about the Digitalmars-d-learn mailing list