Dan*_*lGr 4 c++ using template-specialization c++11 c++14
我想做类似下面的事情(在c ++ 11,c ++ 14;而不是c ++ 17):
template <class T>
using partner = void;
template<>
using partner<A> = X;
template<>
using partner<B> = Y;
template<>
using partner<C> = Z;
Run Code Online (Sandbox Code Playgroud)
但我收到编译错误---
错误:'使用'之前的预期unqualified-id
---关于第一个模板专业化.
这样的事情可能吗?(我已经知道我可以使用带有using语句的模板化类.我希望直接使用using没有类包装器的语句,因为它更简单,更优雅.如果有另一个简单,优雅的解决方案,请分享!)
Bar*_*rry 10
您无法专门化别名模板.
你将不得不求助于普通的,无聊的类模板专业化:
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;
Run Code Online (Sandbox Code Playgroud)
或者我们可以变得更加漂亮
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;
Run Code Online (Sandbox Code Playgroud)