I am trying to use C++11 to define a class which can store several std::function<> and call them depending on their argument types, similar to overload resolution.
I declare a base class for each function signature the 'overloaded' function should support:
template <typename R, typename... A>
struct overload;
template <typename R, typename... A>
struct overload<R(A...)>
{
typedef std::function<R(A...)> F;
F f_;
overload(F f): f_(f) {}
R operator()(A... a)
{
return f_(a...);
}
};
template <typename... T>
struct overloaded_function : public overload<T>...
{
overloaded_function(T... t): overload<T>(t)... {}
};
int main()
{
overloaded_function<void(float), void(int, int)> f([](float){}, [](int, int){});
f(1.F); // build error
f(2, 3); // build error
return 0;
}
Build error: (Visual Studio 2013)
"overload::operator() [with R=void, A=]" is ambiguous c:\Users\benj7280\Documents\kernel_builder\src\main.cpp 39 5 kernel_builder
I don't understand how the operator can be ambiguous since the functions have totally different signatures. The error was exactly the same when I removed the templates altogether, using only concrete classes, and also when I replaced the operator() overload with a named member function.
usings. - T.C.overloadshould just have one parameter with no pack. - T.C.thisin a trailing-return-type, I guess. - dyp