Nested Template Specialization

后端 未结 7 2048
南旧
南旧 2020-12-14 02:12

Having a brain fart... Is it possible to make something like this work?

template struct Foo
{
    template struct Bar;
};

template         


        
7条回答
  •  时光取名叫无心
    2020-12-14 02:59

    As others have pointed out, C++ doesn't let you specialize a nested class if the outer class template is not also specialized. When I had a situation like that, I was able to work around it with a helper class tucked away in an internal namespace:

    namespace internal {
      template  struct FooBarHelper
      { /* ... */ };
      // Specialization
      template  struct FooBarHelper
      { /* specialized version goes here */ };
    }
    
    template struct Foo
    {
      template struct Bar : public internal::FooBarHelper;
    };
    

    Of course it's not quite as hidden as you might like it to be.

提交回复
热议问题