I am trying to get the address of a static method from a class that is passed as a template argument to another class. Below is a pared down example:
#include <iostream>
#include <array>
using namespace std;
typedef size_t Data;
class MyFunction
{
private:
static const std::array<std::string, 3> values;
public:
template<size_t N>
static void Func(const Data& aData)
{
size_t index = (N > aData ? 2 : (N == aData ? 1 : 0) );
cout << "Function::Func<"<< N << ">:\t" << N << values[index] << aData << endl;
}
typedef decltype(&Func<0>) type;
};
const std::array<std::string, 3> MyFunction::values {"<", "=", ">"};
template<class Function, size_t N>
class FunctionManager
{
private:
static const typename Function::type func_;
static constexpr typename Function::type Create()
{
return &Function::Func<N>; //ERROR: Causes "overloaded function with no contextual information".
}
public:
void operator()(const Data &aData) const
{
func_(aData);
}
};
template<class Function, size_t N>
const typename Function::type FunctionManager<Function, N>::func_ = FunctionManager<Function, N>::Create();
int main()
{
static const size_t N = 6;
auto man = FunctionManager<MyFunction, N>();
man(N/2);
return 0;
}
You can also find the code here. The problem is in the Create() function, I get "address of overloaded function with no contextual type information" error. However, if I change &Function::Func to &MyFunction::Func, it works fine. I would like to make the actual function a template parameter and not hard-code it, anyone have any idea how to fix this problem? Note that I realize there are simpler ways of doing what I'm trying to do, but the actual code instead of a single Function::type creates an array of them using the index trick, etc. Any help would be greatly appreciated.