No implicit opOpAssign for structs with basic types?

Robert M. Münch robert.muench at saphirion.com
Sat Apr 4 12:07:53 UTC 2020


On 2020-04-04 10:32:32 +0000, Ferhat Kurtulmuş said:

> Probably I didn't understand what you mean. Sorry if this is not the 
> case, but this one is easy.
> ...
> struct S {
>      float a;
>      float b;
> 
>      S opOpAssign(string op)(ref S rhs) if (op == "+"){
>          this.a += rhs.a;
>          this.b += rhs.b;
>          return this;
>      }
> }
> 
> 
> void main()
> {
>      S a = {1, 5};
>      S b = {2, 5};
> 
>      a += b;
> 
>      writeln(a);
> }
> ...

Yes, sure, but in C++ I don't have to explicitly write this down. It 
just works. IMO that makes a lot of sense as long as all types fit. 
This just looks superfluously.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list