lundi 17 juillet 2017

Explicit specialization in non-namespace scope with variadic template

Consider the following code:

#include <iostream>

using namespace std;

struct foo {
  template <typename> struct bar {
    void operator()() { cout << "other" << endl; }
  };
  template <> struct bar<int> {
    void operator()() { cout << "int" << endl; }
  };
};

int main(int argc, char* argv[]) {
  foo::bar<void>{}();
  foo::bar<int>{}();
}

As expected, I get

test.cc:9:13: error: explicit specialization in non-namespace scope
'struct foo'
   template <> struct bar<int> {

But if I switch the implementation of foo to the following

struct foo {
  template <typename,typename...> struct bar {
    void operator()() { cout << "other" << endl; }
  };
  template <typename... _> struct bar<int,_...> {
    void operator()() { cout << "int" << endl; }
  };
};

It compiles just fine and works.

Does the standard actually allow explicit specialization in non-namespace scope for variadic templates? Why is it disallowed for non-variadic templates?

PS: I'm using GCC 6.2.0.

Aucun commentaire:

Enregistrer un commentaire