Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

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?

like image 554
Michel Avatar asked Dec 06 '12 22:12

Michel


1 Answers

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 ]

like image 54
Dietmar Kühl Avatar answered Nov 09 '22 02:11

Dietmar Kühl