2

テンプレートクラスを構築しようとしていますFod

template<typename S0 = aux::EmptyType, typename S1 = aux::EmptyType, typename S2 = aux::EmptyType, typename S3 = aux::EmptyType, typename S4 = aux::EmptyType, typename S5 = aux::EmptyType, typename S6 = aux::EmptyType, typename S7 = aux::EmptyType, typename S8 = aux::EmptyType, typename S9 = aux::EmptyType>
class Fod { ... };

static const int valueこれには、テンプレート引数のインデックス (S0 の場合は 0、S1 の場合は 1 など) を示すa を持つ内部クラス At が含まれます。まもなく、次の条件を満たす必要があります。

struct Type0 {}; struct Type1 {};
BOOST_STATIC_ASSERT( (Fod<Type0>::At<Type0>::value == 0) );
BOOST_STATIC_ASSERT( (Fod<Type0, Type1>::At<Type0>::value == 0) );
BOOST_STATIC_ASSERT( (Fod<Type0, Type1>::At<Type1>::value == 1) );

私はboost::disable_if次のように使用してみました:

template<class T, class Enable = void>
class At; // undefined

template<>
struct At<S0, typename boost::disable_if<boost::is_same<S0, aux::EmptyType> >::type > {
    static const int value = 0;
};

template<>
struct At<S1, typename boost::disable_if<boost::is_same<S1, aux::EmptyType> >::type > {
    static const int value = 1;
};

template<>
struct At<S2, typename boost::disable_if<boost::is_same<S2, aux::EmptyType> >::type > {
    static const int value = 2;
};

template<>
struct At<S3, typename boost::disable_if<boost::is_same<S3, aux::EmptyType> >::type > {
    static const int value = 3;
};

// and so on for S4...S9

しかし、S3 の特殊化を定義し、両方の S2、S3 が同じ型であるaux::EmptyType場合 (または: S2 の特殊化を定義し、両方の S1、S2 が同じ型である場合) はエラーになります。

4>C:\phd\cpp\src\boost/dst/fod.hpp(144): error C2766: explicit specialization ; 'boost::dst::fod<S0>::At<boost::dst::aux::EmptyType,boost::mpl::s_item<T,Base>>' has already been defined
4>          with
4>          [
4>              S0=Type0
4>          ]
4>          and
4>          [
4>              T=Type0,
4>              Base=boost::mpl::set0<>::item_
4>          ]

問題を解決する方法はありますか?size_t at<S0>()そして、 0 を与えるメソッドが必要な場合は、 size_t at<S1>()1 を与える...?

さらに情報が必要な場合はお尋ねください。

4

3 に答える 3

1

boost :: is_same :: valueが0または1を返すと仮定すると、これに対するより簡単な解決策があります(boolが異なる値を使用する場合は、小さなコンパイル時コンバーターを作成するだけです):現在のAtを次のように置き換えます

template <typename T>
struct At {
    enum {
        value = 
        boost::is_same<T, S0>::value) + 
        boost::is_same<T, S1>::value * 10 + 
        boost::is_same<T, S2>::value * 100
    };
};

これは10進数のビットマスクに評価されます。より広い範囲が必要な場合は、他の値を自由に使用してください。

于 2012-01-19T15:30:57.047 に答える
0

私も最初の質問に答えました。実際、私が思っていたよりも簡単でした:

// main general template (unused or throws a compilation error)
template<class T, class Enable = void >
struct At {};

template<typename T>
struct At<T, typename boost::enable_if< boost::is_same<T, S0> >::type >
{
    static const int value = 0;
};
// and so on for each class template parameter S1,...,S9
于 2012-01-19T14:58:12.040 に答える
0

この回答のおかげで、2番目の質問(テンプレート関数に関する)に部分的に答えることができました:

#include <boost/utility/enable_if.hpp>

template <typename T>
static int at(typename boost::enable_if_c< boost::is_same<T, S0>::value && !boost::is_same<T, aux::EmptyType>::value, T >::type = T())
{
    return 0;
}
// and so on for each class template parameter S1,...,S9
于 2012-01-19T14:02:08.197 に答える