3

次のステートメントを簡単にする方法はありますか? (おそらく、 を使用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ます。

私が書きたいものの例(疑似コード):

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_ifありboost::is_base_ofますか?

4

3 に答える 3

4

まず、次の中から選択する必要があります。

  • 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

于 2010-09-27T07:23:59.823 に答える
4

boost::enable_ifSFINAE はむしろ関数オーバーロードの選択に関するものであるように思われるため、役に立たないと思います。

もちろん、パラメーター付きのテンプレートを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
}
于 2010-09-26T18:01:08.930 に答える
0

ここで 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;
};
于 2012-02-09T23:15:35.023 に答える