C ++ 11非类型模板参数包扩展

时间:2014-01-28 17:37:51

标签: c++ c++11 g++4.8

在使用C ++ 11模板参数包时,我想出了以下代码:

#include <cstdio>

static void testFunc(int i1, int i2) {
    printf("testFunc(%d, %d)\n", i1, i2);
}

template <size_t... Indices> void wrapper() {
    testFunc(Indices...);
}

int main(int argc, char *argv[]) {
    wrapper<1, 2>();    
    return 0;
}

尝试使用g ++ 4.8.2编译它会导致a "too few arguments to function ‘void testFunc(int, int)’"错误。

这是无效的C ++还是g ++还没有实现这种非类型模板参数包的使用?

2 个答案:

答案 0 :(得分:5)

这是有效的,这似乎是gcc的可变参数模板实现中的一个错误。我在gcc bugzilla page上搜索了一下,没有找到任何有关此问题的报告。

答案 1 :(得分:1)

我想作为一种解决方法,你可以这样做:

template <size_t... Indices> void wrapper() {
    constexpr int x[] = {Indices...};
    testFunc(x[0], x[1]);
}
相关问题