How can I compare two C++11 std::function
s with operator==
, and return true
if both of said function
s refer to the same function pointer?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Be aware that equality of functions (deciding if two functions have always the same observable behavior) is an undecidable problem in lambda calculus (and that is why many programming languages forbid comparing functions).
So even if the
==
test compiles, it would at most just test that the code is identical (has the same address), not that the compared functions have the same behaviour.You could try comparing
a
andb
first by comparing their.target_type()
and if these target type ids are the same, then you can compare their.target()
pointers. You can use a mismatching target type as an early out false.If the
std::function<T(U...)> f
is a member function,thefnPointer
will be null.operator== for std::function compares a std::function with a null pointer, as far as I can tell the standard does not provide any details as to why.
Although, this boost FAQ entry, Why can't I compare boost::function objects with operator== or operator!=? provides a rationale and as far as I can tell should be applicable to std::function as well. Quoting the FAQ:
it then outlines requested solutions similar to Preet's and goes on to say:
and explains why this has to has to be dealt with in either the assignment operator or constructor and then goes on to say:
Update
Found a standards rationale in Accessing the target of a tr1::function object, which is pretty old but is consistent with the boost FAQ and says:
You can actually get it to work with
.target
:(Ref: C++ trying to get function address from a std::function)