2015-07-04 73 views
15

我有一个函数foo,它将可变参数函数指针作为参数。预定义可变参数函数指针参数

我想在函数声明之前使用“using”来定义参数的类型。

template <typename ... vARGS> 
using TFuncType = void(*)(vARGS ... V_args); 

template <typename ... vARGS> 
void foo(TFuncType<vARGS ...> funcptr) {} 

void bar(int i) {} 

int main() { 
    foo(&bar); // This line fails to compile. 
} 

这不会编译。错误(通过clang使用C++ 1z)是:

/make/proj/test/variadic-funcparam-deduce2.cpp:39:5: error: no matching function for call to 'foo' 
foo(&bar); 
^~~ 
/make/proj/test/variadic-funcparam-deduce2.cpp:33:36: note: candidate template ignored: substitution failure [with vARGS = int] 
template <typename ... vARGS> void foo(TFuncType<vARGS ...> funcptr) {} 

为什么“int”替换失败?

我可以编译成功,如果我明确地写里面FOO()类型:

template <typename ... vARGS> 
void foo(void(*funcptr)(vARGS ... V_args)) {} 

但我不能获得初始(“使用”)版本,即使明确指定模板参数,并使用工作预铸TFuncType<int>的说法,即:

int main() { 
    TF_call<int> fptr = &bar; // This line is OK. 
    foo<int>(fptr); 
} 

有谁知道这是怎么回事吗?

使用typedef'd(“using”)variadics和/或函数指针我有什么奇怪吗?

+5

适用于GCC,我想不出任何规则这个代码打破了,所以我要去用一个铿锵虫。 –

+0

好吧,我会离开它一个小时左右,然后提交一个错误报告,如果没有进一步的评论。谢谢。 – xaxazak

+0

编译器版本? – Yakk

回答

1

相信这可能与这是我从this answer复制下面的文本本身从C++标准需要在14.5.7 [temp.alias]段2:

当模板id指到别名模板的专业化, 它等同于通过用别名模板的 类型ID替换 其模板参数获得的关联类型。 [注意:别名模板名称永远不会被推断出来。 - 注完]

如果我解释是正确的,这意味着GCC接受的代码实际上是不符合要求的。

+0

是的,我很确定现在它是非法的。你的小片似乎解释它,谢谢。 – xaxazak

+0

我不太确定。它说别名模板*名称*从未被推断。它不是推断名称,因为名称已在模板函数的声明中给出。这个警告似乎只适用于你的模板需要_template template_参数。请参阅[cppreference示例](http://en.cppreference.com/w/cpp/language/template_argument_deduction#Alias_templates) –