简体   繁体   中英

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. They basically say that the call to f() within the templated class needs one argument.

Is this a bug in both compilers, or am I missing something in the C++ standard?

The method taking two parameter packs is illegal according to 14.1 [temp.param] paragraph 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). [ 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 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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