jeudi 4 mai 2017

Member specialization of alias declaration in different namespaces

I just encountered a strange difference in behavior between clang and gcc where I wanted to compile code which looks similar to the following:

namespace n1 {
  template <class T1, class T2>
  struct MyTemplate {
    struct Inner {};
  };
}

using namespace n1;
namespace n2 {
  using MyClass = MyTemplate<int, int>;
}

namespace n1 {
  using n2::MyClass;
  template<> struct MyClass::Inner {
    int member;
  };

  MyClass::Inner inner{0};
}

Clang happily compiles this:

$ clang++ -std=c++11 -c -o alias_specialization.o alias_specialization.cc

but gcc throws the following error:

$ g++ -std=c++11 -c -o alias_specialization.o alias_specialization.cc

alias_specialization:15:30: error: declaration of ‘struct n1::MyTemplate<int, int>::Inner’ in namespace ‘n1’ which does not enclose ‘using MyClass = struct n1::MyTemplate<int, int>’
   template<> struct MyClass::Inner {

I know I can just write the full name of the original type (MyTemplate<int, int>) instead of MyClass in line 15. But I'm simply wondering which of the two compilers is "right". The exact compilers in use are:

$ clang++ --version
clang version 4.0.0

$ g++ --version
g++ (GCC) 6.3.1 20170306

Aucun commentaire:

Enregistrer un commentaire