#include <stdio.h>
template <typename FuncType, FuncType>
struct TransformFunc;
template <typename Arg, typename... Args, void(*func)(Arg, Args...)>
class TransformFunc<void(*)(Arg, Args...), func>
{
public:
static void apply(Arg arg, Args... args)
{
func(arg, args...);
}
};
void test1(int x)
{
printf("test1: int%d\n", x);
}
void test2 (int x, float y)
{
printf("test2: int%d float%f\n", x, y);
}
int main(int, char **)
{
TransformFunc<decltype(&test1), &test1>::apply(5);
TransformFunc<decltype(&test2), &test2>::apply(5, 1.23f); // Error here.
return 0;
}
1>------ Build started: Project: main, Configuration: Debug x64 ------
1> main.cpp
1>main.cpp(33): error C2440: 'specialization' : cannot convert from 'void (__cdecl *)(int,float)' to 'void (__cdecl *)(int)'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast
1> main.cpp(33) : see reference to class template instantiation 'TransformFunc<void (__cdecl *)(int,float),void test2(int,float)>' being compiled
1>main.cpp(33): error C2973: 'TransformFunc<void(__cdecl *)(Arg,Args...),func>' : invalid template argument 'void (__cdecl *)(int,float)'
1> main.cpp(9) : see declaration of 'TransformFunc<void(__cdecl *)(Arg,Args...),func>'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我无法弄清楚为什么test2
实例化无法编译。有什么想法吗?