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

c++, c++11, templates, typedef

Solution

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

template<class T> class Common {
    struct type {
        /// general implementation
    };
};

template<> class Common<Type1> { using type = TypeZ; };
template<> class Common<Type2> { using type = TypeZ; };
template<> class Common<Type3> { using type = TypeZ; };

template<class T> using common_t = typename Common<T>::type;

Then you use `common_t<T>` rather than `Common<T>`.

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

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

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

Problem

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 like to implement them as aliases/typedefs. The following example code should show what I want to do: ``` template<class T> class Common { /// general implementation }; class TypeZ; template<> class Common<Type1> = TypeZ; // <<< how to do this? template<> class Common<Type2> = TypeZ; template<> class Common<Type3> = TypeZ; ``` Is the above possible in some way in C++ (or C++11)? It would be great if I didn't have to implement `Common<...>` as a class that inherits `TypeZ` - the actual code is more complex than shown above and inheriting `TypeZ` is not a good idea there.

Original source