Traits

Artur Skawina art.08.09 at gmail.com
Sat Oct 12 04:42:53 PDT 2013


On 10/11/13 06:13, Agustin wrote:
> I have a function that needs to check if the template provided inherit a class.
> 
> For example:
> 
> public void function(T, A...)(auto ref A values)
> {
>   // static assert(IsBaseOf(L, T));
> }
> 
> Check if T inherit class "L". Same result that std::is_base_of<L, T>::value using C++. Any clean way to do it, without a dirty hack.

As others have said, a "T:L" template arg specialization will often
be enough.

When you really need to restrict the i/f (which can be a good idea),
something like this will work:

   template isBaseOf(BASE, C) {
      static if (is(C S == super))
         enum isBaseOf = {
            foreach (A; S)
               static if (is(A==BASE))
                  return true;
            return is(C==BASE);
         }();
      else
         enum isBaseOf = is(C==BASE);
   }

artur


More information about the Digitalmars-d-learn mailing list