xax*_*zak 15 c++ templates function-pointers variadic c++11
我有一个函数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.
}
Run Code Online (Sandbox Code Playgroud)
这不编译.错误(通过使用c ++ 1z的clang)是:
/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) {}
Run Code Online (Sandbox Code Playgroud)
为什么"int"替换失败了?
如果我在foo()中明确写出类型,我可以成功编译:
template <typename ... vARGS>
void foo(void(*funcptr)(vARGS ... V_args)) {}
Run Code Online (Sandbox Code Playgroud)
但即使明确指定模板参数,并使用预先转换TFuncType<int>的参数,我也无法使初始("使用")版本工作,即:
int main() {
TF_call<int> fptr = &bar; // This line is OK.
foo<int>(fptr);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道这里有什么?
使用我失踪的typedef'd("using")变量和/或函数指针有什么奇怪的吗?
我相信这可能与我从这个答案中复制的以下文本有关,该答案本身取自 14.5.7 [temp.alias] 第 2 段中的 C++ 标准:
\n\n\n\n\n当 template-id 引用别名模板的特化时,它相当于通过用其模板参数替换别名模板的 type-id 中的模板参数而获得的关联类型。[ 注意:永远不会推导出别名模板名称。\xe2\x80\x94\n 尾注]
\n
如果我的解释是正确的,则意味着 GCC 接受的代码实际上是不合格的。
\n