How do I make a template function that only accepts instances of a specific class?

Jack jckj33 at gmail.com
Mon Jan 18 18:40:37 UTC 2021


isInstanceOf from std.traits seems to not work with class the way 
I need to. I'd like to make a template function accepts only 
class of a specified class type

class A { }
class B : A { }
class C : A { }


import std.traits : isInstanceOf;

int f(T)(in A[int] arr)
if(isInstanceOf!(A, T)) // doesn't work for class from what I saw
{
   foreach(c; arr) {
      if((cast(A)c) !is null) {
      }
   }
   // ...
}

int f(T)(in A[int] arr)
if(cast(T) !is null) // run time this is ok but not at compile 
time
{
   // ...
}



More information about the Digitalmars-d-learn mailing list