[c++11] 判断某类是否有某个原型及名称的成员函数



#include "stdafx.h"
#include
#include


template
struct IndexedTWrap
{
 typedef T type;
 static T gettype();
 enum{ Index = _Index };
};

struct _GetClass_HasMember
{
 template
 struct matcher;

 template
 static IndexedTWrap   _hasDefineType(matcher*);
  template
  static IndexedTWrap  _hasDefineType(...);
};
template
struct GetClass_TypeOrT  //获取T::type类型
{
 typedef decltype(_GetClass_HasMember::_hasDefineType(0)) wraptype;
 enum { value = wraptype::Index };
 typedef typename wraptype::type type;
 int view = value;
};

int _tmain(int argc, _TCHAR* argv[])
{
 decltype(&std::string::c_str)* pstr;
 printf("%d %s\n", GetClass_TypeOrT::value, typeid(GetClass_TypeOrT::type).name());  //0
 printf("%d %s\n", GetClass_TypeOrT>::value, typeid(GetClass_TypeOrT>::type).name());  //0
  printf("%d %s\n", GetClass_TypeOrT::value, typeid(GetClass_TypeOrT::type).name());      //1
 return 0;
}


仍然用了decltype,如果需要在低版本使用,请参考http://blog.chinaunix.net/uid-1720597-id-306773.html

你可能感兴趣的:(技术)