关于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(){}
- C/C++ code
IsFunctionT<func> check;
这是在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
[解决办法]