Problem overloading operator for a struct with an immutable member

Jean pierre via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 25 22:44:13 PDT 2015


On Thursday, 26 March 2015 at 04:57:55 UTC, ketmar wrote:
> On Tue, 24 Mar 2015 16:49:01 +0000, Nicolas Sicard wrote:
>
>> I don't know if this is a bug or expected behaviour. The 
>> struct is
>> mutable, assignable and pre-increment operator works. But 
>> post-increment
>> doesn't compile because of the immutable member.
>> 
>> --
>> struct S {
>>      int i;
>>      immutable(Object) o;
>> 
>>      S opUnary(string op)() { return this; }
>>      void opAssign(S other) {}
>> }
>> 
>> void main()
>> {
>>      S s, t;
>> 
>>      t = s; // OK ++s; // OK s++; // Error: cannot modify 
>> struct s S
>>      with immutable members
>> }
>> ---
>
> by the way. do you know that you still CAN overload 
> postincrement
> operation? yes, the code is still here, and it works... 
> somethimes. ;-)

There's might be a bug anyway because even with an alias this:
---
struct S
{
     int i;
     alias i this;
     immutable(Object) o;

     S opUnary(string op)() { return this; }
     void opAssign(S other) {}
}

void main()
{
     S s, t;
     auto j = s.i++; // OK
     auto i = s++; // OUCH, but we expect S.i...
}
---


More information about the Digitalmars-d-learn mailing list