Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine function signature of a callable object?

Is there a way in C++ to determine function signature of a callable object?

Consider following:

template< typename F >
void fun(F f)
{
    // ...
}

Lets assume that fun is called only with callable "things".

Inside of fun I want to know what is the signature of function f. That should work with function pointers, references, wrappers, lambdas, binds, function objects (providing they have only one operator ()) and so on. I'm limited with Visual Studio 2010 SP 1 but am interested in standard solutions even if not working on that compiler.

(A function signature is Return_Type ([Arg1_Type [, Arg2_Type [, ... ] ] ]); same as given to std::function/boost::function.)

A partial solution of knowing at least the return value of f is of some value to. (I have tried std::result_of but couldn't get it to work in any case I tried.)

like image 679
Adam Badura Avatar asked May 09 '11 13:05

Adam Badura


People also ask

How do you find the signature of a function?

We can get function Signature with the help of signature() Function. It takes callable as a parameter and returns the annotation. It raises a value Error if no signature is provided. If the Invalid type object is given then it throws a Type Error.

What is signature of a function example?

A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type.

What is a call signature Python?

A Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a Parameter object in its parameters collection. A Signature object has the following public attributes and methods: return_annotation : object.

What is method signature in C++?

In C/C++, the method signature is the method name and the number and type of its parameters, but it is possible to have a last parameter that consists of an array of values: int printf(const char*, ... );


2 Answers

On C++0x compliant compilers, you can at least get the result type of f() by using decltype(f()). Visual C++ 2010 should support decltype, though I haven't checked it myself yet. As for getting the argument types, I'm not sure if there's a way that would work with function pointers.

Edit

Boost.Function seems to have it figured out, at least on some compilers (it doesn't work on old versions of VC++ or Borland C++ for instance). It can wrap function pointers and extract arguments for them. The solution seems quite complex however, and it involves defining multiple templates with Boost.PP. If you feel like trying to re-implement everything you can certainly try that, but I think you can also just use a dummy Boost.Function wrapper to make things easier, e.g. boost::function<decltype(f)>::second_argument_type to get the second argument type.

like image 83
Boaz Yaniv Avatar answered Oct 02 '22 13:10

Boaz Yaniv


You may look at Boost Function Types:

http://www.boost.org/doc/libs/1_46_1/libs/function_types/doc/html/boost_functiontypes/introduction.html

like image 39
Joel Falcou Avatar answered Oct 02 '22 14:10

Joel Falcou