I hava the following template class like a function wrapper:
using MethodId = std::uint16_t;
template <typename Func>
class BaseMethod
{
public:
BaseMethod(const Func &func, std::uint16_t method_id) : _function(func), _method_id(method_id) {}
template <typename... Args>
auto operator()(Args... args) -> decltype(_function(std::forward<Args>(args)...))
{
return _function(std::forward<Args>(args)...);
}
template <typename... Args>
auto invoke(Args... args) -> decltype(_function(std::forward<Args>(args)...))
{
return _function(std::forward<Args>(args)...);
}
private:
Func _function;
MethodId _method_id;
};
with this class template, I can use like this:
int adder(int x, int y){return x+y;}
BaseMethod<int(*)(int, int)> foo_tempalte(adder, 0);
but I also want to support usage like this:
BaseMethod<int, int, int> bar_tempalte(adder, 0);
So I add a partial specification version:
template <typename Ret, typename... Args>
class BaseMethod<Ret (*)(Args...)>
{
public:
BaseMethod(Ret (*func)(Args...), std::uint16_t method_id) : _function(func), _method_id(method_id)
{
}
Ret operator()(Args... args)
{
return _function(std::forward<Args>(args)...);
}
Ret invoke(Args... args)
{
return _function(std::forward<Args>(args)...);
}
private:
Ret (*_function)(Args...);
std::uint16_t _method_id;
};
but when I called BaseMethod<int, int, int> bar_tempalte(adder, 0);
the ide gave me error: too many arguments for class template "BaseMethod"
I wonder what is wrong with my code? Thanks!
Unless you need to support older compilers (pre-C++17) or really, truly, absolutely need to force the user to explicitly pass template arguments, I'd just let the compiler deduce the function type from argument: