Template parameters that don't affect template type

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 12:28:47 PDT 2016


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
}




More information about the Digitalmars-d-learn mailing list