473,578 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<*obtain ing
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++.m oderated. First time posters: Do this! ]

Feb 9 '07 #1
3 3596
On Feb 9, 7:44 pm, psuj...@gmail.c om 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<*obtain ing
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<typena me F>
void foo(const F& f)
{
std::cout << boost::function _traits<F>::ari ty << std::endl;
}

foo(f);

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Feb 10 '07 #2
In article <11************ **********@s48g 2000cws.googleg roups.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<*obtain ing
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++.m oderated. First time posters: Do this! ]

Feb 10 '07 #3
On 10 Feb., 09:58, Carl Barron <cbarron...@ade lphia.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++.m oderated. 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
2494
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
4047
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 binary format. But what "server()" and "issuer()" return are strings, with the pairs separated by "/". The documentation at...
9
682
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 arity : " << boost::function_traits<*obtaining signature from f()*>::arity << std::endl; please no macro solutions - only C++ (Boost MPL maybe could...
1
1648
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
7778
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6522
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3798
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.