使成员函数的检查适应函数参数

时间:2011-01-23 02:06:49

标签: c++ templates sfinae

我一直在努力调整this 用于实现普通(非成员)功能的解决方案。 在我的例子中,我有很多全局字符串实用程序类型的函数,它们接受任何字符串类型T,这样T就有一个char const* c_str() const成员函数。

如果用户试图传递一些没有c_str()成员函数的类型T,即编译器说“c_str():没有这样的成员函数”,那么目标是消除奇怪的编译器错误消息。 ,我宁愿编译器说,“foo(T&):没有这样的函数”,其中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&) - 为什么不呢?

2 个答案:

答案 0 :(得分:1)

如果我正确理解了问题,请将enable_if应用于f本身 以下将解决问题:

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

希望这有帮助。

答案 1 :(得分: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 ) {}