3

クラスに特定のサブクラス/タイプがある場合、SFINAEのトリックを知ってもらうことはできますか?何かのようなもの、

template<typename TYPE> // searches for "my_type"
struct has_inner_type {
  enum { value = <???> };
};

次に例を示します。

struct A {
  class my_type {};  // has_inner_type::value = true 
};
struct B { }; // has_inner_type::value = false
struct C { typedef int my_type; }; // has_inner_type::value = true

私はいくつかのトリックを試しましたが、ほとんどの場合、予想されるコンパイラエラーで不十分でした。使用法:

bool b = has_inner_type<A>::value;  // with respect to "my_type"

編集my_type: 2番目のパラメータとしてに渡すことは不可能のようであるため、質問を再編集しましたhas_inner_type。したがって、現時点で問題となるのは、特定のタイプのみを検索することmy_typeです。私はこのコードを試しましたが、機能しません。

4

1 に答える 1

0

以下は私が質問に投稿したウィキペディアのリンクにあった答えです!! (@nmに感謝します。)

template <typename T> 
struct has_inner_type
{
  typedef char yes[1];
  typedef char no[2];

  template <typename C> static yes& test(typename C::my_type*);
  template <typename> static no& test(...);

  static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};

これがデモです。

于 2011-07-02T04:05:13.790 に答える