How can I point a function with function overload?

Viewed 76
#include <stdio.h>
#include <functional>

int foo(int x)
{
    return x;
}

int foo(int a, int b)
{
    return a + b;
}

int main()
{
    std::function<int(int)> guiFunc2 = foo;      //error : no suitable constructor exists to convert from "<unknown-type>" to "std::function<int(int)>"
    std::function<int(int, int)> guiFunc1 = foo; //error : no suitable constructor exists to convert from "<unknown-type>" to "std::function<int(int, int)>"

    return 0;
}

I want to make two function pointers to functions with same name but this code does not work.

It's easy to just change the functions name but I would like to know if it's possible to make funtion pointers with same name.

Thanks.

2 Answers

Cast the address to correct type before assignment:

std::function<int(int)> guiFunc2 = static_cast<int(*)(int)>(foo);
std::function<int(int, int)> guiFunc1 = static_cast<int(*)(int, int)>(foo);
Related