質問

次のステートメントを簡素化する方法はありますか? (おそらく、使用 boost::enable_if).

私はシンプルなクラス構造を持っています - Base 基本クラス、 Derived1, Derived2 継承から Base.

次のコードがあります。

template <typename Y> struct translator_between<Base, Y> {
   typedef some_translator<Base, Y> type;
};

template <typename Y> struct translator_between<Derived1, Y> {
   typedef some_translator<Derived1, Y> type;
};

template <typename Y> struct translator_between<Derived2, Y> {
   typedef some_translator<Derived2, Y> type;
};

1つのテンプレートの専門化を使用して同じステートメントを書きたい translator_between.

私が書きたいものの例(pseudocode):

template <typename Class, typename Y>

ONLY_INSTANTIATE_THIS_TEMPLATE_IF (Class is 'Base' or any derived from 'Base')

struct translator_between<Class, Y> {
   typedef some_translator<Class, Y> type;
};

これを使用して達成する方法 boost::enable_ifboost::is_base_of?

役に立ちましたか?

解決

まず、次の選択肢を選択する必要があります。

  • is_base_of
  • is_convertible

どちらもで見つけることができます <boost/type_traits.hpp>, 、後者はより寛容です。

何らかの組み合わせのためにこのタイプのインスタンス化を単純に防止する場合は、静的アサートを使用します。

// C++03
#include <boost/mpl/assert.hpp>

template <typename From, typename To>
struct translator_between
{
  BOOST_MPL_ASSERT((boost::is_base_of<To,From>));
  typedef translator_selector<From,To> type;
};

// C++0x
template <typename From, typename To>
struct translator_between
{
  static_assert(boost::is_base_of<To,From>::value,
                "From does not derive from To");
  typedef translator_selector<From,To> type;
};

ここで過負荷解像度が行われていないため、必要ありません enable_if.

他のヒント

とは思わない boost::enable_if Sfinaeは、関数の過負荷間でむしろ選択することについてむしろむしろ役立つためです。

もちろん、テンプレートを使用できます bool 選択を改善するためのパラメーター:

#include <boost/type_traits.hpp>
class Base {};

class Derived : public Base {};

template <class A, class B>
struct some_translator {};

template <typename A, typename B, bool value>
struct translator_selector;  //perhaps define type as needed

template <typename A, typename B>
struct translator_selector<A, B, true>
{
    typedef some_translator<A, B> type;
};

template <typename A, typename B>
struct translator_between
{
    typedef typename translator_selector<A, B, boost::is_base_of<Base, A>::value>::type type;
};

int main()
{
    translator_between<Base, int>::type a;
    translator_between<Derived, int>::type b;
    translator_between<float, int>::type c;  //fails
}

Anable_ifとこのマクロを使用して、より読みやすくすることができます。

#define CLASS_REQUIRES(...) typename boost::enable_if<boost::mpl::and_<__VA_ARGS__, boost::mpl::bool_<true> > >::type

次に、このようなクラスを定義できます。

template <typename Class, typename Y, class Enable = 
CLASS_REQUIRES(boost::is_base_of<Class, Y>)> 
struct translator_between {
    typedef some_translator<Class, Y> type;
};
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top