One type traits class (or something like that) to get the return type of a pointer to function

Discussion in 'C++' started by Diego Martins, Sep 4, 2006.

  1. Hi all.

    I want to build a template class to extract the return type of a
    pointer to function

    Something like:

    template<typename CB>
    class ReturnTypeExtractor {
    typedef ReturnType .... ??? :-(
    };

    ....
    // user code

    typedef FooObject * (*CallBackType)();

    typedef ReturnTypeExtractor<CallBackType> myReturnType; //
    myReturnType is FooObject *

    How can I accomplish this?

    Thanks In Advance

    Diego
    HP
     
    Diego Martins, Sep 4, 2006
    #1
    1. Advertising

  2. Diego Martins

    Kai-Uwe Bux Guest

    Diego Martins wrote:

    > Hi all.
    >
    > I want to build a template class to extract the return type of a
    > pointer to function
    >
    > Something like:
    >
    > template<typename CB>
    > class ReturnTypeExtractor {
    > typedef ReturnType .... ??? :-(
    > };
    >
    > ...
    > // user code
    >
    > typedef FooObject * (*CallBackType)();
    >
    > typedef ReturnTypeExtractor<CallBackType> myReturnType; //
    > myReturnType is FooObject *
    >
    > How can I accomplish this?


    Here is some code I use:

    template < typename F >
    struct nullary_function_traits {

    typedef typename F::result_type result_type;

    };

    template < typename R >
    struct nullary_function_traits< R (*)( void ) > {

    typedef R result_type;

    };


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 5, 2006
    #2
    1. Advertising

  3. Diego Martins

    Pete Becker Guest

    Re: One type traits class (or something like that) to get the returntype of a pointer to function

    Diego Martins wrote:
    >
    > I want to build a template class to extract the return type of a
    > pointer to function
    >
    > Something like:
    >
    > template<typename CB>
    > class ReturnTypeExtractor {
    > typedef ReturnType .... ??? :-(
    > };
    >
    > ...
    > // user code
    >
    > typedef FooObject * (*CallBackType)();
    >
    > typedef ReturnTypeExtractor<CallBackType> myReturnType; //
    > myReturnType is FooObject *
    >
    > How can I accomplish this?
    >


    #include <functional>

    std::tr1::result_of<CallBackType>::type;

    That is, if you have TR1. For details, see section 6.4 of my book, "The
    Standard C++ Library Extensions: a Tutorial and Reference." You can get
    a complete implementation of TR1 from Dinkumware, www.dinkumware.com.

    result_of might also be available from boost. It was developed from a
    couple of similar things there, and they've made some adaptations to
    support TR1.

    The problem in rolling your own is that you can't write a template that
    takes an arbitrary number of arguments, so you can't easily extract the
    return type unless you know how may arguments the function takes:

    template <class Ty>
    struct result0;

    template <class Ret>
    struct result0<Ret(*)()>
    {
    typedef Ret type;
    };

    template <class Ty>
    struct result1;

    template <class Ret, class Arg0>
    struct result1<Ret(*)(Arg0)>
    {
    typedef Ret type;
    };

    and so on.
     
    Pete Becker, Sep 5, 2006
    #3
  4. Diego Martins

    Noah Roberts Guest

    Pete Becker wrote:

    > result_of might also be available from boost. It was developed from a
    > couple of similar things there, and they've made some adaptations to
    > support TR1.


    It certainly is. That's where it came from. Most of TR1 was first
    created in boost...you should know that.
     
    Noah Roberts, Sep 5, 2006
    #4
  5. Diego Martins

    Pete Becker Guest

    Re: One type traits class (or something like that) to get the returntype of a pointer to function

    Noah Roberts wrote:
    > Pete Becker wrote:
    >
    >
    >>result_of might also be available from boost. It was developed from a
    >>couple of similar things there, and they've made some adaptations to
    >>support TR1.

    >
    >
    > It certainly is. That's where it came from. Most of TR1 was first
    > created in boost...you should know that.
    >


    I certainly do know that much of TR1 came from Boost. That doesn't mean
    that Boost provides everything that's in TR1.

    As I said, result_of came from a couple of things in Boost. My
    recollection is that those things were somewhat different from what
    eventually turned into result_of. I haven't looked to see whether the
    final version of std::tr1::result_of is available from Boost. I'm not
    going to say that it is, just because it's based on things from there.

    --
    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
    For more information about this book, see www.petebecker.com/tr1book.
     
    Pete Becker, Sep 5, 2006
    #5
  6. Kai-Uwe Bux wrote:
    > Diego Martins wrote:
    >
    > > Hi all.
    > >
    > > I want to build a template class to extract the return type of a
    > > pointer to function
    > >
    > > Something like:
    > >
    > > template<typename CB>
    > > class ReturnTypeExtractor {
    > > typedef ReturnType .... ??? :-(
    > > };
    > >
    > > ...
    > > // user code
    > >
    > > typedef FooObject * (*CallBackType)();
    > >
    > > typedef ReturnTypeExtractor<CallBackType> myReturnType; //
    > > myReturnType is FooObject *
    > >
    > > How can I accomplish this?

    >
    > Here is some code I use:
    >
    > template < typename F >
    > struct nullary_function_traits {
    >
    > typedef typename F::result_type result_type;
    >
    > };
    >
    > template < typename R >
    > struct nullary_function_traits< R (*)( void ) > {
    >
    > typedef R result_type;
    >
    > };
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    thank you! your solution is great for dealing with callbacks without
    parameters

    thanks all for the tr1 and boost hints, but I don't want to install any
    addicional libraries now (due project policies and my personal taste
    for making stuff manually . it is a good way to learn things)

    by the way, there is a good link I've found somewhere in another thread
    http://erdani.org/publications/traits.html

    Cheers
    Diego
    HP
     
    Diego Martins, Sep 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    4
    Views:
    483
    Guest
    Oct 13, 2004
  2. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
  3. greek_bill
    Replies:
    3
    Views:
    538
    greek_bill
    Nov 2, 2008
  4. ray
    Replies:
    1
    Views:
    1,332
    Robert Kern
    Jun 4, 2010
  5. Qi
    Replies:
    1
    Views:
    397
Loading...

Share This Page