You've stumbled on a gray area, but the long and short of things is that what you're trying to do is illegal in standard C++, and all of the direct workarounds are also illegal. An explicit specialization needs to be in the enclosing namespace scope, which would place it outside your class:
namespace A {
template <typename T>
struct B {
template <typename X>
struct G;
};
template <typename T>
template<>
struct B<T>::G<int>;
}
The problem is that this is also illegal because you aren't allowed to specialize the nested class without also specializing the containing class. This compiles, but limits you severely:
namespace A {
template <typename T>
struct B {
template <typename X>
struct G;
};
template<>
template<>
struct B<int>::G<int>;
}
If B weren't a template class, this wouldn't be a problem. The end result is that you're stuck with indirect fixes like wrapping the whole of B and its "nested" classes in a nested namespace:
namespace A {
namespace B {
template <typename T>
struct B {
};
template <typename X>
struct G;
template<>
struct G<int>;
}
}
Now the problem goes away because you're not trying to nest a specialized class inside a template class, and you still have the benefit of a unique scope for B, G, and all of G's specializations.