4

最近、私は C++11 ライブラリを開発しており、実際にユーザーに公開されるクラスが、含まれている型に基づいて継承されたクラスを決定するこのパターンを数回使用するようになりました。ここでは、これを達成するために使用する va_if 可変引数メタ関数を再現しましたが、これはboost::mpl::if_cまたはを使用しても達成できますstd::conditional

template<typename bool_type, typename result_type, typename... Ts>
struct va_if;

template<typename result_type, typename... Ts>
struct va_if<std::true_type, result_type, Ts...>
{
  typedef result_type type;
};

template<typename result_type, typename... Ts>
struct va_if<std::false_type, result_type, Ts...>
{
  typedef typename va_if<Ts...>::type type;
};

template<typename T>
class container_base { /* Generic container functions */ };

template<typename T>
class container_integral_base
  : public container_base<T>
{ /* Code tailored to integral types */ };

template<typename T>
class container_floating_point_base
  : public container_base<T>
{ /* Code tailored to floating point types */ };

// This class chooses the class it should inherit from at compile time...    
template<typename T>
class Container
  : public va_if<std::is_integral<T>::type, container_integral_base<T>,
                 std::is_floating_point<T>::type, container_floating_point_base<T>>::type
{ /* public interface code */ };

私が疑問に思っているのは、継承のコンパイル時の決定のこのパターンには名前がありますか?

4

1 に答える 1