我有一个这样的程序:
template<typename ...Args>
using Function = void(*)(Args *...);
template<typename ...Args>
void DoThing(Function<Args...> func) { }
void IntFunction(int *i) { }
int main(int argc, char *argv[]) {
DoThing(IntFunction);
}
当我运行程序时,我收到此错误
$ clang++ -std=c++14 template.cpp
template.cpp:12:3: error: no matching function for call to 'DoThing'
DoThing(IntFunction);
^~~~~~~
template.cpp:7:6: note: candidate template ignored: substitution failure [with Args = int]
void DoThing(Function<Args...> func) { }
^
1 error generated.
但如果我使用g ++编译它,我就不会有任何错误。
在类型别名中使用时,clang似乎无法推断出可变参数模板参数。如果我用标准参数替换可变参数,那么我就不会再出现错误。
哪个编译器给我正确的结果?为什么我不被允许这样做?
答案 0 :(得分:0)
可以缩减为
template <typename... T>
using funptr = void(*)(T...);
template <typename... T>
void f(funptr<T...>) {}
template void f(void(*)());
这是有效的代码;如果我们用funptr<T...>
替换相应的包装扩展,Clang突然不再抱怨了。
报告为#25250。