Is there a way to know at compile time, which version of an overloaded function has been chosen by the compiler to be called?

99 Views Asked by At

A possible way of doing this is to make the return type of the overloaded functions store the information of the specific function:

template<class _ret_t, class _func_t>
struct tpinf
{
    using ret_t = _ret_t;
    using func_t = _func_t;

    ret_t value;
};


tpinf<int, int(*)(double)> func(int n) { ... }
tpinf<int, int(*)(char)> func(char c) { ... }

int main()
{
    int n = 56;

    using resolved_function_type = decltype(func(n))::func_t;

    auto v = func(n).value; // access returned object

    return 0;
}

The problem is, I might want to know which overload is resolved of some predefined or standard functions, for that I have to rewrite those functions, which is not a good idea. So is there a more efficient way to achieve this?

1

There are 1 best solutions below

3
Caleth On

You can take the address of an overloaded function in a context where which overload you want is unambiguous.

int func(int);
int func(char);

int main()
{
    int n = 56;

    int (*pfunc)(int) = func; // pfunc is a pointer to 'int func(int)'

    return 0;
}