Function traits at compile time

Daniel Keep daniel.keep.lists at gmail.com
Mon Jun 12 10:26:04 PDT 2006



Oskar Linde wrote:
> Daniel Keep skrev:
>> Victor Nakoryakov wrote:
>>> Hi all,
>>
>> Hi.
>>
>>> Consider code:
>>>
>>> class MyClass(StructT)
>>> {
>>> ....
>>> }
>>>
>>> I know, that StructT has a function `foo` in it, but I don't know number
>>> of parameters and their types, of course. Does anybody knows a way to
>>> extract types to use them in `static if ( is(...) )` statements?
>>
>> Ok, this is ugly, and evil, evil hack, but it works.  The python script
>> that generated it is included if you need more and/or less arguments.
>>
> 
> Nice set of templates. Makes one wish D had variadic templates though. :)

Oh-so-true.

>> Some examples:
>>
>> # NumberOfArgs!(&StructT.foo); // Number of arguments
>> # ReturnType!(&StructT.foo);   // Alias to return type
>> # ArgType!(&Struct.foo, n);    // Alias to type of 'n'th argument
>> #                              // (counting from 1).
> 
> I don't think those will work as typed. Unless I'm mistaken, you need
> typeof(&StructT.foo) instead of just &StructT.foo. And unless
> StructT.foo is static, you will need to add delegate (as different from
> function) overloads for ftype.d and use typeof(&StructT.init.foo).

You're absolutely right.  I am an idiot.

Forgive me, I haven't touched it in a while, and I've got cryptanalysis
on the brain for my last exam.  Stupid university...

You DO indeed need to use ``Template!(typeof(&StructT.foo))``.

However, I'm not sure how to fix the function pointer vs. delegate
problem.  AFAI understand the rules, you can't have two templates with
the same arguments and use IFTI (hell, I don't think you can have two
templates with the same arguments period).

I suppose that if you picked apart the mangled name of the type, you
could determine if it was a delegate or a function pointer, and then
select the correct sub-template to extract the type.  Whoo boy... this
thing's getting huge.  Guess I'll have to add a new template to my type
traits library :P

Walter, can't we just have variadic templates and/or compile-time
reflection?  I mean, that's gotta be easier then working with this
unholy mess :P

> Apart from that, I must thank you for coming up with this inspired way
> of (ab)using the ifti support. I've found it much useful.

Oh yes, I was quite pleased with myself when the proverbial light bulb
switched on.  Rather like the time I worked out how to do pointers in
VB6 ^_^.  No reason just, y'know, because.

> Regards,
> 
> Oskar

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list