Is it possible to define an implementation template specialization as typedef of another type?

前端 未结 2 950
你的背包
你的背包 2021-01-17 14:15

I have a class template for which I want to introduce several template specializations. Those template specializations identical to some existing type. Conceptually I would

2条回答
  •  情深已故
    2021-01-17 14:41

    Assuming only certain specialisations of Common are aliases of TypeZ then you can do:

    template class Common {
        struct type {
            /// general implementation
        };
    };
    
    template<> class Common { using type = TypeZ; };
    template<> class Common { using type = TypeZ; };
    template<> class Common { using type = TypeZ; };
    
    template using common_t = typename Common::type;
    

    Then you use common_t rather than Common.

    Just to entertain the inheritance idea, have you tried this?

    template<> class Common : public TypeZ { using TypeZ::TypeZ; };
    template<> class Common : public TypeZ { using TypeZ::TypeZ; };
    template<> class Common : public TypeZ { using TypeZ::TypeZ; };
    

    Then you don't need to use a nested type alias.

提交回复
热议问题