1

私は 、通常の (非メンバー) 関数の存在を可能にするために、このソリューションを適応させようとしています。char const* c_str() const私の場合、T がメンバー関数を持っているなど、任意の文字列型 T を取るグローバル文字列ユーティリティ型関数がたくさんあります。

目標は、ユーザーがメンバー関数を持たない型 T を渡そうとした場合の奇妙なコンパイラ エラー メッセージを排除することですc_str()。つまり、コンパイラが「c_str(): そのようなメンバー関数はありません」と言うのではなく、 , "foo(T&): no such function"fooはグローバル関数です。

適応されたコードは次のとおりです。

template<bool B,typename T = void>
struct enable_if {
  typedef T type;
};

template<typename T>
struct enable_if<false,T> {
};

//
// This macro is adapted from the linked-to question -- this part works fine.
//
#define DECL_HAS_MEM_FN(FN_NAME)                                            \
  template<class ClassType,typename MemFnSig>                               \
  struct has_##FN_NAME##_type {                                             \
    typedef char yes[1];                                                    \
    typedef char no[2];                                                     \
    template<typename T,T> struct type_check;                               \
    template<class T> static yes& has( type_check<MemFnSig,&T::FN_NAME>* ); \
    template<class T> static no&  has( ... );                               \
    enum { value = sizeof( has<ClassType>(0) ) == sizeof( yes ) };          \
  }

// Declare an instance of the above type that checks for "c_str()".
DECL_HAS_MEM_FN(c_str);

// Define another macro just to make life easier.
#define has_c_str(STRINGTYPE) \
  has_c_str_type<STRINGTYPE,char const* (STRINGTYPE::*)() const>::value

//
// A "ValidatedStringType" is a StringType that uses the above machinery to ensure that
// StringType has a c_str() member function
//
template<class StringType>
struct ValidatedStringType {
  typedef typename enable_if<has_c_str(StringType),StringType>::type type;
};

// Here's the global function where I want to accept only validated StringTypes.
template<class StringType>
void f( typename ValidatedStringType<StringType>::type const& s ) {
}

struct S { // Class for testing that has c_str().
  char const* c_str() const {
    return 0;
  }
};

struct N { // Class for testing that does not have c_str().
};

using namespace std;

int main() {
  S s;
  N n;
  cout << has_c_str(S) << endl; // correctly prints '1'
  cout << has_c_str(N) << endl; // correctly prints '0'
  f( s ); // error: no matching function for call to 'f(S&)'
}

ただし、上に示したように、コンパイラは「認識」しf(S&)ません。

4

2 に答える 2

1

質問を正しく理解していれば、次のように自分自身に適用enable_ifするfと問題が解決します。

template<class StringType>
typename enable_if<has_c_str(StringType),StringType>::type
f( StringType const& s ) {....}

お役に立てれば。

于 2011-01-23T12:51:01.727 に答える
0

私はあなたの質問に答えることができません(「なぜですか?」)が、回避策を提供することはできます。しかし、男、それは醜いです。最終的に、すべての関数の構造と関数を定義します。

template<class StringType, class T = typename ValidatedStringType<StringType>::type>
struct f_helper {
  void operator()( T const& s ) {
      // Put the contents of f() here
  }
};

template<class StringType>
void f(StringType const &s) {
  f_helper<StringType>()( s );
}

ボイラープレートの一部を削除するために作成できる前処理の魔法があると確信していますが、それはかなり醜いものでもあります。

#define DECL_VALIDATED_FUNC( RetType, name, Validator, contents ) \
  template<class StringType, class T = typename Validator<StringType>::type> \
  struct name ## _helper { \
    RetType operator()( T const& s ) contents \
  }; \
  \
  template<class StringType> \
  RetType name(StringType const& s) { \
     name ## _helper<StringType>()( s ); \
}

DECL_VALIDATED_FUNC( void, f, ValidatedStringType, {
   // put contents of f() here
}) // note the closing parenthesis

残念ながら、関数を解放するためのデフォルトのテンプレート引数を指定することはできません。

template<class StringType, class T = typename ValidatedStringType<StringType>::type>
void f( T const& s ) {}
于 2011-01-23T03:56:58.023 に答える