Why is partial specialization of a nested class template allowed, while complete isn't?

后端 未结 4 2067
遇见更好的自我
遇见更好的自我 2020-11-29 03:10
    template struct A {                                                                                                    
        template

        
4条回答
  •  失恋的感觉
    2020-11-29 03:55

    Backing up Virgil's argument (he was faster than I posting the same rationale), consider this:

    template
    class TOuter
      {
      public:
        template
        class TInner
          {
          public:
            T1 m_1;
            T2 m_2;
          };
      };
    
    template
    template<>
    class TOuter::TInner
      {
      public:
        T1    m_1;
        float m_2;
     };
    

    Is TInner fully specialized, or partially specialized because of T1?

    Edit:

    After considering some of the other comments - it seems that you want to have a full specialization based on the template parameter in the outer class. If you nest the inner class's implementation, that seems to work in Visual Studio 2005:

    template
    class TOuter
      {
      public:
        template
        class TInner
          {
          public:
            std::string DoSomething() { return "Inner - general"; }
            T2 m_2;
          };
    
        template<>
        class TInner
          {
          public:
            std::string DoSomething() { return "Inner - special"; }
            T1 m_1;
          };
      };
    

    TOuter::TInner will correctly be the specialization of TInner. I could not get it to compile with the implementation outside of the template.

提交回复
热议问题