如何计算给定的表达式(function<void()> == functor)。
代码示例:
#include <functional>
using namespace std;
void Foo() { }
int main() {
    function<void()> a;
    // if (a == Foo) -> error
}
编辑:调试细节,并删除图片。
std::function::target()将检索指向存储的可调用对象的指针。这就是你想要比较的。
您必须指定期望的存储类型,如果类型不匹配则target()返回。nullptr
(这意味着如果std::function持有函数指针,target()将返回一个指向函数指针的指针。)
auto f = Foo; // This pointer existed in an earlier version of the question
if (*it->target<decltype(f)>() == f) { // Success
请注意,因为函数衰减为函数指针:
*it->target< decltype(Foo) >() == Foo
不会起作用,因为std::function不可能持有该功能的副本Foo。您需要衰减该函数,例如:
*it->target< std::decay_t<decltype(Foo)> >() == Foo
或者:
*it->target< decltype(&Foo) >() == Foo
| 归档时间: | 
 | 
| 查看次数: | 160 次 | 
| 最近记录: |