检查两个std::函数是否相等

时间:2022-12-02 14:09:13

If I have two std::functions, how can I check whether both hold the same function or not?

如果我有两个std:::函数,如何检查两个函数是否都包含相同的函数?

Additional Information: I have a vector of functions std::vector<std::function<void()>> and before adding another function to the vector I want to check if it already is contained.

附加信息:我有一个函数向量std::向量 <::> >,在向向量添加另一个函数之前,我想检查它是否已经包含。 函数

2 个解决方案

#1


4  

I dont think that they both can be compared. Here is an example to explain some points on std::function comparison

我不认为两者可以相提并论。这里有一个例子来解释std:::函数比较

#2


-1  

Generally std::function and boost::function are not comparable because they need their stored object be comparable but not all function objects and also lambdas have operator== so std or boost functions have no way to compare them

一般来说,std::function和boost:::function是不可比较的,因为它们需要存储的对象是可比较的,但不是所有的函数对象,而且lambdas有操作符==,所以std或boost函数没有办法对它们进行比较

#1


4  

I dont think that they both can be compared. Here is an example to explain some points on std::function comparison

我不认为两者可以相提并论。这里有一个例子来解释std:::函数比较

#2


-1  

Generally std::function and boost::function are not comparable because they need their stored object be comparable but not all function objects and also lambdas have operator== so std or boost functions have no way to compare them

一般来说,std::function和boost:::function是不可比较的,因为它们需要存储的对象是可比较的,但不是所有的函数对象,而且lambdas有操作符==,所以std或boost函数没有办法对它们进行比较