Access derived type in baseclass static function template

Timoses via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 2 06:11:26 PDT 2017


On Wednesday, 2 August 2017 at 12:49:12 UTC, Steven Schveighoffer 
wrote:
> On 8/2/17 8:07 AM, Timoses wrote:
>> Hey,
>> 
>> wondering whether it's possible to access the derived type 
>> from a function template in the base class or interface.
>> 
>> this T does not seem to be working, I guess because it's a 
>> static function and this does not exists?!
>
> Yep.
>
>> Any way I could accomplish this?
>
> Move test outside the interface:
>
> void test(C)(C c) if(is(C : I))
> {
>    writeln(C.type.stringof);
> }
>
> Though, at that point, you don't need I. I'm assuming you have 
> a more complex real-world example.
>
> -Steve

Thanks for the reply!

Not sure I understand correctly, though.

interface I {}
class A : I {}

void test(T)(T t) if(is(T: I))
{ writeln(T.type.stringof); }

void main()
{
     A.test;
}

throws:
   Error: no property 'test' for type 'app.A'

which it of course does... test(A) makes no sense either.
I need to call it statically and want to know the derived class 
type in the base class (or interface) function template.

Is there a trick in your answer? : P


More information about the Digitalmars-d-learn mailing list