第十九章 19.3 辨别函数类型
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename U> static One test(...);
template<typename U> static Two test(U (*)[1]);
public:
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};
这里的代码有一处错误,导致无法编译,应该修改为
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template<typename U> static One test(...);
template<typename U> static Two test(U (*)[1]);
public:
enum { Yes = sizeof(IsFunctionT<T>::test<T>(0)) == 1 };
enum { No = !Yes };
};
本文介绍了一种使用模板元编程技术来检测C++中函数类型的方法,并提供了一个示例代码片段,该代码存在一处编译错误。通过修正错误,可以正确地判断一个类型是否为函数。

被折叠的 条评论
为什么被折叠?



