例如返回一个函数指针,如何使用下面的pf呢
auto pf(const string &, const string &) -> bool (*)(const string &s1, const string &s2);
bool LengthCompare(const string &temp1, const string &temp2)
{
return temp1 < temp2;
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我认为题主你肯定有什么搞错了。首先在C++里面,函数pf接受两个string,然后返回一个函数指针(重点:他不是闭包),这个新函数又接受了两个string然后返回bool。这种函数几乎是没有什么意义的,除非两个string的目的就是查找一个函数来返回。
一般来讲,尾置返回类型是为了使用decltype,或者纯粹让代码变得更可读。譬如说上面那个pf,就有另外一种写法:
decltype的例子如下,譬如说你可能不知道a+b到底返回什么类型:
这样你不仅可以Add(1, 2),还可以Add(3.0, 4.0f),甚至还可以Add("VczhIsAGenius", 7)了。
最后,如果你说拿到这样的一个pf应该怎么使用,那肯定跟下面的写法差不多:
我建议你重新审视一下你的题目。