Concepts? Template specialization depending on class functions?

Oskar Linde olREM at OVEnada.kth.se
Thu Mar 2 14:42:30 PST 2006


Ben Phillips wrote:

> In article <du751p$1imp$1 at digitaldaemon.com>, Ben Phillips says...
>>
>>Is it possible to determine whether a given class contains a specific
>>member function?
>>
> 
> From reading the docs it seems like I should be able to do this using
> "is", but the appropriate syntax is unclear and I can't seem to figure it
> out. Does "is" make it possible to test of a class has a certain member
> function and, if so, what is the correct syntax?

I'm not fully sure of the syntax, but this seems to work:

To test if a class T contains a method test: 

is(typeof(T.test) == function)

Or conceptualized:

template Testable(T) {
        const Testable = is(typeof(T.test) == function));
}

One could wish that you could write something like:

is(typeof(T.test) == int function(T))

To make sure that the function signature is OK, but this does not seem to
work.

Instead, the following works kind of but doesn't check the exact type of the
arguments. To check if T contains a method with the signature similar to
int test(int,int,int):

is(typeof((new T).test(1,1,1)) == int)

But will be true for e.g. a int test(real,real,real) too...

/Oskar



More information about the Digitalmars-d mailing list