65,186
社区成员




template<typename T>
class IsFunction
{
private:
typedef struct {} One;
typedef struct { One a[2]; } Two;
template<typename U> static One testFunction(...);
template<typename U> static Two testFunction(U (*)[1] );
public:
enum { YES = sizeof(IsFunction<T>::testFunction<T>(0)) == sizeof(One) };
enum { NO = !YES };
};
void fun() {}
template<typename T>
void checkFun(T *t)
{
if(IsFunction<T>::YES)
cout << "is function!" << endl;
else
cout << "is not function!" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Fun myFun = &fun;
int *pa;
checkFun(pa); // {1}
checkFun(myFun); // {2}
//IsFunction<cl>::YES;
getchar();
return 0;
}
template<typename T>
class IsFunction
{
private:
typedef struct {} One;
typedef struct { One a[2]; } Two;
static One testFunction(...);
static Two testFunction(U (*)[1] );
public:
enum { YES = sizeof(IsFunction<T>::testFunction(0)) == sizeof(One) };
enum { NO = !YES };
};
template<typename T>
class IsFunction
{
private:
typedef struct {} One;
typedef struct { One a[2]; } Two;
template<typename U> static One testFunction(...);
template<typename U> static Two testFunction(U (*)[1] );
public:
enum { YES = sizeof(testFunction<T>(0)) == sizeof(One) };
enum { NO = !YES };
};
typedef struct {} One;
typedef struct { One a[2]; } Two;
template<typename U> static One testFunction(...);
template<typename U> static Two testFunction(U (*)[1] );
template< typename T >
struct IsFunction
{
enum { value = 0 };
};
template< typename T, typename... U >
struct IsFunction< T( U... ) >
{
enum { value = 1 };
};
// If the input template argument is a type that allows to be supplemented a function to, like did to a structure or a class or a
// union, the result will be defined as non-zero.
template < typename _T_ >
struct Is_Single_Non_Scalar_Object
{
private:
template < typename _t_ >
static char (& GetRes(void (_t_::*)(void)))[2];
template < typename _t_ >
static char GetRes(...);
public:
enum nRes { result = static_cast<int>(sizeof(GetRes<_T_>(nullptr))) - 1 };
Is_Single_Non_Scalar_Object(void) = delete;
Is_Single_Non_Scalar_Object(const Is_Single_Non_Scalar_Object &) = delete;
};
数组的识别用普通的型式特化就可以识别出来(估计你很快就想得到,所以代码就不贴了)
然后内嵌一个结构,并特化出一个版本筛选掉数组和非标量等,仅仅留下标量类型进行分析,也就是把楼主的代码都移入结构中的非特化版本部分里去。
template<typename T>
class IsFunction
{
private:
typedef struct {} One;
typedef struct { One a[2]; } Two;
template<typename U> static One testFunction(...);
template<typename U> static Two testFunction(U(*)[1]);
public:
enum { YES = sizeof(IsFunction<T>::template testFunction<T>(nullptr)) == sizeof(One) };
enum { NO = !YES };
};
void fun() {}
template<typename T>
void checkFun(T *t)
{
if (IsFunction<T>::YES)
std::cout << "is function!" << std::endl;
else
std::cout << "is not function!" << std::endl;
}
class A
{
public:
virtual ~A() = 0;
};
int main(void)
{
auto myFun = &fun;
int *pa = nullptr;
A * pb = nullptr;
checkFun(pa); // <span style="color: #FF0000;">{1}</span>
checkFun(pb); // <span style="color: #FF0000;">{1}</span>
checkFun(myFun); // <span style="color: #FF0000;">{2}</span>
//IsFunction<cl>::YES;
_getch();
return(0);
}
输出结果:
C:\MinGW\bin>test
is not function!
is function!
is function!
class A
{
public:
void A::Foo()
{
}
};
+1 [quote=引用 10 楼 vipcxj 的回复:] [quote=引用 9 楼 supermegaboy 的回复:] [quote=引用 8 楼 vipcxj 的回复:] [quote=引用 5 楼 supermegaboy 的回复:] [quote=引用 4 楼 bbs2241 的回复:] [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?
+1 [quote=引用 9 楼 supermegaboy 的回复:] [quote=引用 8 楼 vipcxj 的回复:] [quote=引用 5 楼 supermegaboy 的回复:] [quote=引用 4 楼 bbs2241 的回复:] [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?
+1 [quote=引用 8 楼 vipcxj 的回复:] [quote=引用 5 楼 supermegaboy 的回复:] [quote=引用 4 楼 bbs2241 的回复:] [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?
+1 [quote=引用 5 楼 supermegaboy 的回复:] [quote=引用 4 楼 bbs2241 的回复:] [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?
+1 [quote=引用 4 楼 bbs2241 的回复:] [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?
+1 [quote=引用 2 楼 Adol1111 的回复:] [quote=引用 1 楼 Automation_dmu 的回复:] 1中 U类型 哪里来?