1.函数类型推导
#include <iostream>
bool MyComp(int val1, int val2)
{
return val1 > val2;
}
int main()
{
decltype(MyComp) fun2;
fun2 = MyComp;
}
error: non-object type ‘decltype(MyComp)’ (aka ‘bool (int, int)’) is not assignable
相关参考:https://www.cnblogs.com/levylaw/p/16479800.html
分析:decltype()
可以避免退化, 所以使用decltype(MyComp)
后,MyComp不会退化为函数指针,所以依然是函数指针对象,对象类型是’bool (int, int)’,此处是函数对象的声明,(函数对象是纯右值?),不能进行赋值。
修改为:
#include <iostream>
bool MyComp(int val1, int val2)
{
return val1 > val2;
}
int main()
{
decltype(&MyComp) fun2;
fun2 = MyComp;
}
等价于
#include <iostream>
bool MyComp(int val1, int val2)
{
return val1 > val2;
}
int main()
{
using FuncPtr_10 = bool (*)(int, int);
FuncPtr_10 fun2;
fun2 = MyComp;
return 0;
}
分析:fun2现在被声明为’bool (*)(int, int)’是函数指针类型,fun2是函数指针,能进行赋值。fun2 = MyComp;
此处操作符=右的MyComp会隐式转换为函数指针,等价于fun2 = &MyComp;
.
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/276680.html