473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Obtaining function signature

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 '07 #1
3 3579
On Feb 9, 7:44 pm, psuj...@gmail.com 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! ]

Feb 10 '07 #2
In article <11**********************@s48g2000cws.googlegroups .com>,
<ps*****@gmail.comwrote:
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 Tstruct 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 Fstruct 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! ]

Feb 10 '07 #3
On 10 Feb., 09:58, Carl Barron <cbarron...@adelphia.netwrote:
template <class Tstruct 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 Fstruct 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 Tstruct 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! ]

Feb 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: seannakasone | last post by:
Is there a way to get the callstack level in c++? for example, take the following code: void call3() { //callstack level would be 3 } void call2() { //callstack level would be 2 call3();
11
by: John Nagle | last post by:
The Python SSL object offers two methods from obtaining the info from an SSL certificate, "server()" and "issuer()". The actual values in the certificate are a series of name/value pairs in ASN.1...
9
by: psujkov | last post by:
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...
1
by: psujkov | last post by:
Hi everybody, in addition to my previous question, first of all this : template<typename F> void foo(const F& f) { std::cout << boost::function_traits<F>::arity << std::endl; }
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.