gpt4 book ai didi

c++ - 标准运算符的函数指针

转载 作者:行者123 更新时间:2023-11-28 02:35:30 24 4
gpt4 key购买 nike

<分区>

这是 function pointers using '<' as an operator 的后续问题仍然没有答案。

这与我在此处找到的答案代码非常相似:Is it possible to get the function pointer of a built-in standard operator?

问。以上链接中+用作 std::operator+那为什么我不能使用 std::operator<

目标是不使用参数较少的重载函数,而是用 std::library 中的内容替换

template <typename T>

bool compare (T a,T b,bool (*compareFunction) (T ,T ))
{
return compareFunction (a,b);
}

template <typename T>
bool compare (T a,T b)
{
return a<b;
}

bool m (int a ,int b)
{
return a<b;
}
int main ()
{
int a=5,b=6;

/*
* This Works
*/

if (compare<int>(a,b,m))
cout<<"Sucess";
else
cout<<"Post this in stack overflow ";

/*
* This also Works
*/

if (compare<int>(a,b))
cout<<"Sucess";
else
cout<<"Post this in stack overflow ";

/*
* Adding this gives compile error.
*/

if (compare<int> (a,b,&std::operator<))
cout<<"sucess";
else
cout<<"Post in Stackoverflow ";

return 0;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com