大佬教程收集整理的这篇文章主要介绍了c – 为什么无法将函数指针与模板函数进行比较?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
void func(int) {} template<typename T> void templatedFunc(T) {} int main() { void (*p)(int) = func; bool test1 = p==func; //bool test2 = p==templatedFunc<int>; // compilation error bool test3 = p==&templatedFunc<int>; // but this works }
如果您取消对test2行的注释,并尝试使用g编译代码,则会收到以下错误:
test.cpp: In function ‘int main()’: test.cpp:8:21: error: assuming cast to type ‘void (*)(int)’ from overloaded function [-fpermissive] bool test2 = p==templatedFunc<int>; // compilation error ^~~~~~~~~~~~~~~~~~
我得到这个结果在g 5.3.0和6.2.0.同时,编译与cl 3.6.0成功,没有警告.
哪个编译器根据标准这里是正确的,这给出错误或cl,哪些不?
你看到从(1.1)到(1.7)的列表中缺少什么…内置的运算符!
如果你声明一个operator ==的重载,那么gcc不会抱怨比较,比你不必明确地专门化模板函数:
void func(int) {} template<class T> void templatedFunc(T) {} struct s{}; bool operator==(s,void(*)(int)){return false;} int main() { void (*p)(int) = templatedFunc; bool test1 = p==func; bool test2 = s{} == templatedFunc<int>; // no error - no overload resolution bool test3 = s{} == templatedFunc; // no error - overload resolution bool test4 = p == templatedFunc<int>; // gcc error,but not an error - // no overload resolution //bool test5 = p == templatedFunc; // error - overload resolution not // performed for built-int operators }
test2和test3使用gcc进行编译. test4没有在gcc上编译,但是没有重载解析,你明确专门的功能.真的应该编译test5不按照标准进行编译.在这种情况下,gcc会产生与test4完全相同的错误信息.这肯定是一个gcc错误.
以上是大佬教程为你收集整理的c – 为什么无法将函数指针与模板函数进行比较?全部内容,希望文章能够帮你解决c – 为什么无法将函数指针与模板函数进行比较?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。