GCC:函数包装器模板问题

mok*_*oka 5 c++ gcc templates clang c++11

我正在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;
}
Run Code Online (Sandbox Code Playgroud)

我在gcc上遇到的错误如下:

prog.cpp:9:46:错误:'Ret(Args ...)'不是模板非类型参数的有效类型struct FunctionWrapper ^ prog.cpp:在函数'int main()'中:prog.cpp :20:45:错误:'int(int,int)'不是模板非类型参数FunctionWrapper包装器的有效类型;

关于如何使这两者兼顾的任何想法,clanggcc

谢谢!

Bar*_*rry 8

我认为这是一个gcc bug.根据[temp.param]:

将"数组T"或函数类型的非类型模板参数T调整为"指向T" 的类型.

具有Ret(Args...)作为模板的非类型参数等同于具有Ret(*)(Args...)作为模板的非类型参数.

请注意,gcc [正确]编译以下示例,这与您的原始版本基本相同:

static int testFunc(int _a, int _b)
{
    return _a + _b;
}

template <int F(int, int)>
struct Foo { };


int main() {
    Foo<testFunc> wrapper;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

作为一种解决方法,两个编译器都允许简单地强制非类型参数为指针:

template<class Sig, Sig* F>
struct FunctionWrapper;


template<class Ret, class... Args, Ret (*Func)(Args...)>
struct FunctionWrapper<Ret(Args...), Func>
{ };
Run Code Online (Sandbox Code Playgroud)

但我不认为这应该是必要的.