【发布时间】:2016-10-06 13:59:35
【问题描述】:
我在GCC 5.3 上试图让一些函数包装器代码工作,这在clang 上运行良好。这是一个简单的例子:
#include <iostream>
using namespace std;
template<class Sig, Sig F>
struct FunctionWrapper;
template<class Ret, class... Args, Ret (*Func)(Args...)>
struct FunctionWrapper<Ret(Args...), Func>
{
};
static int testFunc(int _a, int _b)
{
return _a + _b;
}
int main() {
FunctionWrapper<int(int, int), testFunc> wrapper;
return 0;
}
我在 gcc 上遇到的错误如下:
prog.cpp:9:46: 错误:'Ret(Args ...)' 不是模板非类型参数的有效类型 结构函数包装器 ^ prog.cpp:在函数'int main()'中: prog.cpp:20:45: 错误:'int(int, int)' 不是模板非类型参数的有效类型 FunctionWrapper 包装器;
关于如何在clang 和gcc 上进行这项工作的任何想法?
谢谢!
【问题讨论】:
-
即使在 g++6.1 中也不起作用
-
我认为,与其直接使用
Ret(Args...),不如在尝试对函数指针类型进行任何复杂操作时使用 typedef 可能会取得更大的成功。在匹配模板模式时,编译器似乎对协调函数与函数指针转换感到困惑。例如。template <class Ret, class... Args> using free_function = Ret(*)(Args...);
标签: c++ templates c++11 gcc clang