多个可变参数

时间:2012-12-17 17:03:19

标签: c++ templates c++11

我试图做这样的事情:

template <class ... Required>
class Base
{
    template <class First, class ... Rest>
    void bar(First f, Rest ... r)
    {
        [...]
        return bar(r...);
    }
    void bar()
    {
        return;
    }
    public:
        template <class ... Optional>
        void foo(Required ... r, Optional ... o)
        {
            [...]
            bar(r...); //separate the required from the optional
            bar(o...);
        }
};

class Child : Base<Foo1, Foo2>
{
    public:
        Child()
        {
            [...]
            foo(foo1,foo2,foo3);
        }
}

但第一个bar调用是接收所有参数而不是仅接收Required个参数,第二个调用不接收任何参数。我是否想念多个可变参数?难道编译器不应该知道Required...Foo1,Foo2而其余的是Optional吗?

1 个答案:

答案 0 :(得分:1)

我认为这很可能是您使用的任何编译器中的错误。我用gcc 4.6.3和4.7.2以及clang 3.0和3.3试了一下,除了clang 3.0之外,它们都产生了预期的输出。