Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare the signature of two functions?

Is there a way to check if two functions have the same signature? For example:

int funA (int a, int b);
int funB (int a, int b);
float funC (int a, int b);
int funD (float a, int b);

In this example, funA and funB is the only combination of functions that should return true.

like image 530
pallagommosa Avatar asked Nov 29 '19 09:11

pallagommosa


People also ask

What is signature in function?

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. exceptions that might be thrown or passed back.

How do you compare two functions in Python?

cmp() is an in-built function in Python, it is used to compare two objects and returns value according to the given values. It does not return 'true' or 'false' instead of 'true' / 'false', it returns negative, zero or positive value based on the given input.

What are the parts of a function signature?

A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.


4 Answers

Essentially you want to check if types of two functions are the same:

std::is_same_v<decltype(funA), decltype(funB)>

I wouldn't call this 'comparing signatures', as, if I remember correctly, return type is not a part of a signature (because it doesn't affect overload resolution).

like image 121
HolyBlackCat Avatar answered Oct 13 '22 08:10

HolyBlackCat


You can check the function type with decltype and std::is_same. e.g.

std::is_same_v<decltype(funA), decltype(funB)>  // true 

LIVE

like image 31
songyuanyao Avatar answered Oct 13 '22 07:10

songyuanyao


Others have mentioned the solution using std::is_same and decltype.

Now to generalize the comparison for an arbitrary number of function signatures, you can do the following

#include <type_traits> // std::is_same, std::conjunction_v

template<typename Func, typename... Funcs>
constexpr bool areSameFunctions = std::conjunction_v<std::is_same<Func, Funcs>...>;

and compare as many functions as one like

areSameFunctions<decltype(funA), decltype(funB), decltype(funC)>

(See Live Demo)


Or for less typing (i.e. without decltype), make it as a function

template<typename Func, typename... Funcs>
constexpr bool areSameFunctions(Func&&, Funcs&&...)
{
   return std::conjunction_v<std::is_same<Func, Funcs>...>;
}

and call simply by

areSameFunctions(funA, funB, funC) 

(See Live Demo)

like image 45
JeJo Avatar answered Oct 13 '22 07:10

JeJo


As another possibility that hasn't been mentioned: you can use typeid from typeinfo and ==:

#include <typeinfo>

if(typeid(funA) != typeid(funB))
    std::cerr << "Types not the same" << std::endl;
like image 30
S.S. Anne Avatar answered Oct 13 '22 08:10

S.S. Anne