Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-06-13 23:43:23


Arkadiy Vertleyb wrote:
> "Kirit Sælensminde" <kirit.saelensminde_at_[hidden]> wrote
>
>> I've been playing around with a few functional programming idioms in C++
>> using Boost.function and Boost.lambda and was wondering if it was
>> possible to fetch out the argument type (i.e. int) from a type like this:
>>
>> void (*somefunc)( int )
>
> You don't need any libraries for this -- just use partial template
> specialization:
>
> template<class T> struct deduce_arg; //not defined
>
> template<class A> struct deduce_arg<void(*)(A0)>
> {
> typedef A0 type;
> };

This is cool. I'm sure I've tried this sort of thing in MSVC 7.1 before
and not had it work. I guess I got the syntax all mixed up.

You have a typo in the deduction part. A0 should be A (or A should be
A0). It also needs to be a little different to work with the member
function (I've added the return type R to the deduction, but the
important bit is the extra specialisation):

template< typename T > struct deduce_arg;
template< typename R, typename A > struct deduce_arg< R (*) (A) >
{
        typedef R ret;
        typedef A arg1;
};
template< typename C, typename R, typename A > struct deduce_arg< R
(C::*) (A) >
{
        typedef R ret;
        typedef A arg1;
};

Thanks for the idea.

K


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net