首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C++ templates中的判别函数类型 几点疑问?求解~

2012-07-28 
关于C++ templates中的辨别函数类型 几点疑问?求解~~书中的相关部分源码:C/C++ codetemplate typename T

关于C++ templates中的辨别函数类型 几点疑问?求解~~
书中的相关部分源码:

C/C++ code
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/C++ code
 void func(){} 
若要鉴别func是否为函数指针,则应这样调用
C/C++ code
IsFunctionT<func>  check;
此时:模板参数为:T = void (*func)() 按C++函数重载规则,则会先匹配第二个test,那么此时test中参数如何解释?

这是在vs中调试出的错误信息:
错误10error C2056: 非法表达式e:\暑期学习\c++ templates\tpyeofdemo\tpyeofdemo\type2.h151TpyeOfDemo
错误2error C2783: “IsFunctionT<T>::One IsFunctionT<T>::test(...)”: 未能为“U”推导 模板 参数e:\暑期学习\c++ templates\tpyeofdemo\tpyeofdemo\type2.h151TpyeOfDemo
错误1error C2784: “IsFunctionT<T>::Two IsFunctionT<T>::test(U (*)[1])”: 未能从“int”为“U (*)[1]”推导 模板 参数e:\暑期学习\c++ templates\tpyeofdemo\tpyeofdemo\type2.h151TpyeOfDemo





[解决办法]
探讨

你还是弄错了,应该是Yes = sizeof(test<T>(0)) == 1

热点排行