C ++参数包无法扩展

时间:2015-04-01 16:10:31

标签: c++ c++11 gcc variadic-templates

我正在使用可变参数模板,我无法理解为什么以下代码无法编译(GCC 4.9.2 with std = c ++ 11):

这只是一个例子,但我需要在我的代码中使用类似的方法,它也会失败:

template<int I>
class Type{};
template<typename ... Tlist>
class A{
    public:
        template<int ...N>
            void a(Type<N> ... plist){

            }
};
template<typename ... Tlist>
class B{
public:
    template<int ... N>
        void b(Type<N> ... plist){
            A<Tlist...> a;
            a.a<N...>(plist ...);
        }
};

使用示例:

B<int, int, int> b;
b.b<1,7,6>(Type<1>(),Type<7>(),Type<6>());

我收到以下错误:

file.cpp: In member function ‘void B<Tlist>::b(Type<N>...)’:
file.cpp:58:9: error: expected ‘;’ before ‘...’ token
    a.a<N...>(plist ...);
         ^
file.cpp:58:24: error: parameter packs not expanded with ‘...’:
    a.a<N...>(plist ...);
                        ^
file.cpp:58:24: note:         ‘N’

但是下面的代码编译得很好(我刚从两个类中删除了Tlist参数并相应地调整了代码):

template<int I>
class Type{};
class A{
    public:
        template<int ...N>
            void a(Type<N> ... plist){

            }
};
class B{
public:
    template<int ... N>
        void b(Type<N> ... plist){
            A a;
            a.a<N...>(plist ...);
        }
};


B b;
b.b(Type<1>(),Type<7>(),Type<6>());

任何人都可以给我一个解释吗? 感谢。

1 个答案:

答案 0 :(得分:3)

编译器没有理由相信a.a是模板;因此,它被强制将<解释为小于运算符。写:

        a.template a<N...>(plist ...);
          ^^^^^^^^^

在第二个示例中,它知道a.a是一个模板,因为a的类型不依赖于模板参数。

相关问题