简体   繁体   English

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

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

I am scratching my head with a strange problem highlighted by the following minimal code: 我正在摸索着一个由以下最小代码突出显示的奇怪问题:

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
}

Both g++ and clang++ give the same basic error messages for the second case. g ++和clang ++都为第二种情况提供了相同的基本错误消息。 They basically say that the call to f() within the templated class needs one argument. 他们基本上说模板化类中对f()的调用需要一个参数。

Is this a bug in both compilers, or am I missing something in the C++ standard? 这是两个编译器中的错误,还是我在C ++标准中遗漏了什么?

The method taking two parameter packs is illegal according to 14.1 [temp.param] paragraph 11: 根据14.1 [temp.param]第11段,采用两个参数包的方法是非法的:

... A template parameter pack of a function template shall not be followed by another template parameter unless that template parameter can be deduced from the parameter-type-list of the function template or has a default argument (14.8.2). ...函数模板的模板参数包不能跟随另一个模板参数,除非该模板参数可以从函数模板的参数类型列表中推导出来或者具有默认参数(14.8.2)。 [ Example: [例如:

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

—end example ] - 末端的例子]

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

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