简体   繁体   中英

Specialising a variadic template member function of a non-template class

I'm having a problem with this code:

#include <iostream>

using namespace std;

class A {
public:
        template<typename T, typename... Args>
        void stuff(Args... args);
};

template<typename T, typename... Args>
void A::stuff(Args... args) {
        cout << sizeof...(args) << endl;
}

template<>
void A::stuff<int>() {
        cout << "int" << endl;
}

int main() {
        A a;
        A b;

        a.stuff<char>();
        b.stuff<int>();
}

Trying to compile it , I get this error:

template-id 'stuff<int>' for 'void A::stuff()' does not match any template declaration

What am I doing wrong? I tried it without the variadicness and it worked, but how do I specialise a variadic template member function?

This looks like a bug. The problem is not limited to fully-specialized member function templates. It can be reproduced even with free-function templates as follows:

template<typename T, typename... Args>
void stuff2(Args... args);

template<typename T, typename... Args>
void stuff2(Args... args) {
    cout << sizeof...(args) << endl;
}

template<>
void stuff2<int>() {
    cout << "int" << endl;
}
int main() {}

While clang 3.2 compiles this just fine, gcc complains about:

spec.cpp:31:6: error: template-id 'stuff2' for 'void stuff2()' does not match any template declaration

There is a related SO question .

A message seems to confirm that this is indeed a bug.

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