Obtaining function signature

Discussion in 'C++' started by psujkov@gmail.com, Feb 9, 2007.

  1. Guest

    Hi everybody,

    int f(int a, int b) { return a + b; };
    is it possible to obtain this function signature - int (int, int) in
    this case - for use in boost::function_traits ?
    e.g. std::cout << "f's arity : " << boost::function_traits<*obtaining
    signature from f()*>::arity << std::endl;
    please no macro solutions - only C++ (Boost MPL maybe could be useful
    - but still don't see how)

    best regards, Paul Sujkov


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    , Feb 9, 2007
    #1
    1. Advertising

  2. Carl Barron Guest

    In article <>,
    <> wrote:

    > Hi everybody,
    >
    > int f(int a, int b) { return a + b; };
    > is it possible to obtain this function signature - int (int, int) in
    > this case - for use in boost::function_traits ?
    > e.g. std::cout << "f's arity : " << boost::function_traits<*obtaining
    > signature from f()*>::arity << std::endl;
    > please no macro solutions - only C++ (Boost MPL maybe could be useful
    > - but still don't see how)
    >
    > best regards, Paul Sujkov

    template <class T> struct function_arity;

    template <class R>
    struct function_arity<R()> {static const int value = 0;};

    // for N=1..N_max do
    template <class R,class T1>
    struct function_arity<R(T1)> {static const int value = 1;};

    template <class R,class T1,class T2>
    struct function_arity<R(T1,T2)> {static const int value = 2;};
    // etc.

    boost's preprocessor library can be used to automate this, but
    you said no preprocessor stuff.

    using boost function [which uses the preprocessor :)]
    template <class F> struct function_arity
    {
    static const int value = boost::function<F>::arity;
    };
    but tr1::function does not provide this.


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Carl Barron, Feb 10, 2007
    #2
    1. Advertising

  3. On Feb 9, 7:44 pm, wrote:

    > int f(int a, int b) { return a + b; };
    > is it possible to obtain this function signature - int (int, int) in
    > this case - for use in boost::function_traits ?
    > e.g. std::cout << "f's arity : " << boost::function_traits<*obtaining
    > signature from f()*>::arity << std::endl;
    > please no macro solutions - only C++ (Boost MPL maybe could be useful
    > - but still don't see how)


    Couldn't you do

    template<typename F>
    void foo(const F& f)
    {
    std::cout << boost::function_traits<F>::arity << std::endl;
    }

    foo(f);



    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    Mathias Gaunard, Feb 10, 2007
    #3
  4. On 10 Feb., 09:58, Carl Barron <> wrote:
    > template <class T> struct function_arity;
    >
    > template <class R>
    > struct function_arity<R()> {static const int value = 0;};
    >
    > // for N=1..N_max do
    > template <class R,class T1>
    > struct function_arity<R(T1)> {static const int value = 1;};
    >
    > template <class R,class T1,class T2>
    > struct function_arity<R(T1,T2)> {static const int value = 2;};
    > // etc.
    >
    > boost's preprocessor library can be used to automate this, but
    > you said no preprocessor stuff.
    >
    > using boost function [which uses the preprocessor :)]
    > template <class F> struct function_arity
    > {
    > static const int value = boost::function<F>::arity;
    > };
    > but tr1::function does not provide this.


    Once variadic templates are accepted, then we have a cool,
    short, concise (have I forgotten any relevant attribute? ;-))
    approach to realize this:

    template <class T> struct function_arity;

    template <class R, class... Ts>
    struct function_arity<R(Ts...)> { static const std::size_t value =
    sizeof...(Ts); };

    Greetings from Bremen,

    Daniel Kr├╝gler




    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
     
    =?iso-8859-1?q?Daniel_Kr=FCgler?=, Feb 10, 2007
    #4
    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. JJBW
    Replies:
    1
    Views:
    10,448
    Joerg Jooss
    Apr 24, 2004
  2. Replies:
    9
    Views:
    398
    Victor Bazarov
    Feb 27, 2007
  3. Replies:
    1
    Views:
    405
  4. Gustavo Narea
    Replies:
    14
    Views:
    886
    Gustavo Narea
    Feb 16, 2009
  5. daniel rich
    Replies:
    5
    Views:
    325
    daniel rich
    Nov 14, 2012
Loading...

Share This Page