考虑以下代码,它使用具有可变参数的函数:
#include <iostream>
// Typedef function type
template<typename... Output>
using Func = void(Output*...);
// Function runner
template<typename... Output>
void run_func(Func<Output...>& func, Output*... output) {
for (int i=0 ; i < 10 ; ++i) {
func(output...);
}
}
void f(double* d) {
*d *= 2;
};
int main() {
double value = 1.0;
run_func(f, &value);
printf("%f\n", value);
}
使用 g++ 4.7.3 编译此代码没有问题,运行后会生成预期的结果 1024.0
。
但是如果使用 icpc 14.0.2 编译,则会导致程序崩溃...
templ.cc(21): internal error: assertion failed: lower_expr: bad kind (shared/cfe/edgcpfe/lower_il.c, line 18582)
run_func(f, &value);
^
使用clang 3.5.0-1编译会导致以下错误消息:
templ.cc:21:3: error: no matching function for call to 'run_func'
run_func(f, &value);
^~~~~~~~
templ.cc:9:6: note: candidate template ignored: deduced conflicting types for parameter 'Output' ('double' vs. <double>)
void run_func(Func<Output...>& func, Output*... output) {
^
这是一个错误吗?还是说g++不应该编译这个?
为什么clang会推断出这些“冲突”的类型,例如<double>
是否表示未打包的参数列表?
更新 icpc 14.0.3没有崩溃,程序可以正确编译和运行。