7

プリプロセッサ以外に、明示的なテンプレートのインスタンス化を条件付きで有効/無効にするにはどうすればよいですか?

検討:

template <typename T> struct TheTemplate{ /* blah */ };

template struct TheTemplate<Type1>;
template struct TheTemplate<Type2>;
template struct TheTemplate<Type3>;
template struct TheTemplate<Type4>;

コンパイル条件によっては、Type3 は Type1 と同じであり、Type4 は Type2 と同じです。これが発生すると、エラーが発生します。タイプが同じであり、Type3 と Type4 でインスタンス化されていないことを検出したい

// this does not work
template struct TheTemplate<Type1>;
template struct TheTemplate<Type2>;
template struct TheTemplate<enable_if<!is_same<Type1, Type3>::value, Type3>::type>;
template struct TheTemplate<enable_if<!is_same<Type2, Type4>::value, Type4>::type>;

私は enable_if と SFINAE を試して気をそらしました (そして、それらが失敗する理由を知っていると思います) が、プリプロセッサだけが機能しました (うーん)。タイプをタプルまたは変数に入れ、重複を取り除き、残りをインスタンス化に使用することを考えています。

テンプレート引数の型に基づいて明示的なテンプレートのインスタンス化を条件付きで有効/無効にする方法はありますか?

4

1 に答える 1

7
template <typename T> struct TheTemplate{ /* blah */ };

template<int> struct dummy { };

template struct TheTemplate<Type1>;
template struct TheTemplate<Type2>;
template struct TheTemplate<conditional<is_same<Type1, Type3>::value, dummy<3>, Type3>::type>;
template struct TheTemplate<conditional<is_same<Type2, Type4>::value, dummy<4>, Type4>::type>;

これでも 4 つの明示的なインスタンス化が生成されますが、 is が ( is でない限り)Type3と同じ場合は重複しません。Type1Type1dummy<3>

于 2012-12-18T10:36:24.570 に答える