Search code examples
c++templatesfunction-pointers

how to use std::function to point to a function template


#include <functional>

int func(int x, int y)
{
    return x + y;
}

int main()
{
    typedef std::function<int(int, int)> Funcp;
    Funcp funcp = func;

    return 0;
}

But is it possible to point to a template function?

#include <functional>

template<class T>
T func(T x, T y)
{
    return x + y;
}

int main()
{
    typedef std::function<?(?, ?)> Funcp;
    Funcp funcp = func;

    return 0;
}

Solution

  • No. A template function is exactly that, a template. It's not a real function. You can point a std::function to a specific instantiation of the template function, e.g. func<int,int>