Obtaining function signature

P

psujkov

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
 
C

Carl Barron

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.
 
M

Mathias Gaunard

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);
 
?

=?iso-8859-1?q?Daniel_Kr=FCgler?=

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top