Template parameters that don't affect template type

Engine Machine via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 13:17:49 PDT 2016


On Thursday, 11 August 2016 at 19:28:47 UTC, Lodovico Giaretta 
wrote:
> On Thursday, 11 August 2016 at 18:11:30 UTC, Engine Machine 
> wrote:
>> [...]
>
> If, in your case, it is possible to use one type as the other, 
> then specify it.
> I mean, implement a templated opAssign that allows you to 
> assign values of one instantiation to values of another. While 
> doing so, remember that this will not change the behaviour of 
> the assigned-to variable, but will only transfer the runtime 
> state from one variable to the other.
>
> struct S(T1, T2)
> {
>     T1 t;
>     void opAssign(T)(auto ref S!(T1, T) other)
>     {
>         t = other.t;
>     }
> }
> unittest
> {
>     S!(int, float) x(1);
>     S!(int, char) y(3);
>
>     x = y;
>     assert(x.t == 3);                                // the 
> value of x is changed
>     static assert(is(typeof(x) == S!(int, float)))   // the 
> type of x isn't changed
> }

Ok, well, my point is that in some cases, the OpAssign is 
trivially since everything is copied. I guess being explicit is 
not a bad thing in this case though.




More information about the Digitalmars-d-learn mailing list