I would like to do something like the following (in c++11, c++14; not c++17):
template <class T>
using partner = void;
template<>
using partner<A> = X;
template<>
using partner<B> = Y;
template<>
using partner<C> = Z;
But I get a compilation error---
error: expected unqualified-id before ‘using’
---on the first template specialization.
Is such a thing possible? (I already know I can use a templated class with a using
statement inside it. I'm hoping directly use the using
statement without the class wrapper, since it's simpler and more elegant. If there's another simple, elegant solution, please share!)
You can't specialize alias templates.
You'll have to resort to normal, boring class template specialization:
template <class T> struct partner_t { using type = void; };
template <> struct partner_t<A> { using type = X; };
template <> struct partner_t<B> { using type = Y; };
template <> struct partner_t<C> { using type = Z; };
template <class T>
using partner = typename partner_t<T>::type;
Or we could get fancier
template <class T> struct tag { using type = T; };
template <class T> auto partner_impl(tag<T> ) -> tag<void>;
auto partner_impl(tag<A> ) -> tag<X>;
auto partner_impl(tag<B> ) -> tag<Y>;
auto partner_impl(tag<C> ) -> tag<Z>;
template <class T>
using partner = typename decltype(partner_impl(tag<T>{}))::type;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With