繁体   English   中英

模板化和非模板化类的可变参数模板方法行为的令人费解的差异

[英]Puzzling difference of variadic template method behavior for templated and non templated classes

我正在摸索着一个由以下最小代码突出显示的奇怪问题:

struct A {
    template <typename ...X, typename ...Y>
    void f(X... a, Y...b) {
    }

    template <typename ...X>
    void g(X...c) {
       f<X...> (c...);
    }
};

template <typename T>
struct B {
    template <typename ...X, typename ...Y>
    void f(X... a, Y...b) {
    }

    template <typename ...X>
    void g(X...c) {
       f<X...> (c...);
    }
};



int main() {
    A a;
    a.g(); // Compiles without problem

    B<int> b;
    b.g(); // Compiler complains saying g() calls f<>() with 0 arguments while 1 is expected
}

g ++和clang ++都为第二种情况提供了相同的基本错误消息。 他们基本上说模板化类中对f()的调用需要一个参数。

这是两个编译器中的错误,还是我在C ++标准中遗漏了什么?

根据14.1 [temp.param]第11段,采用两个参数包的方法是非法的:

...函数模板的模板参数包不能跟随另一个模板参数,除非该模板参数可以从函数模板的参数类型列表中推导出来或者具有默认参数(14.8.2)。 [例如:

template<class T1 = int, class T2> class B; // error
// U cannot be neither deduced from the parameter-type-list nor specified
template<class... T, class... U> void f() { } // error
template<class... T, class U> void g() { } // error

- 末端的例子]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM