opCast / operator overloading with additional template arguments

Ali Çehreli acehreli at yahoo.com
Mon Jan 11 16:26:48 UTC 2021


On 1/10/21 7:27 PM, Paul wrote:

 > On Monday, 11 January 2021 at 02:37:24 UTC, Ali Çehreli wrote:
 >> >>     T opCast(T)() const if (is(T : Vec!(size, S2), S2)) {
 >
 >> The is expression can be so complicated that I used a different
 >> approach below.
 >
 >>   if (isInstanceOfVec!T &&
 >>       T.init.content.length == size) {
 >>     // ADDED:
 >>     alias S2 = typeof(T.init.content[0]);
 >
 > Is it ok to use .init even though we don't need an instantiated value?

Yes, .init useful in many templates. However, you don't actually use 
that object because typeof does not evaluate the expression, just 
produces the type of it.

Related, the content[0] expression might be seen as invalid code because 
the array may not have any elements at all but again, there is no access 
to element 0. There are other ways e.g.

     import std.range;
     alias S2 = ElementType!(typeof(T.init.content));

Ali




More information about the Digitalmars-d-learn mailing list