I use the standard shell functions from the C ++ 11 library, and I see strange behavior with its logical operator. If I create an object std::function, the boolean operator returns false. This is true if I assign an nullptrobject to an object and validate it again. The problem occurs when I assign her a void pointer, which I entered in the function pointer. Consider the following program:
#include <functional>
#include <iostream>
void* Test() {
return nullptr;
}
int main(int argc, char* argv[]) {
std::function<void()> foo;
std::cout << !!foo << std::endl;
foo = nullptr;
std::cout << !!foo << std::endl;
foo = reinterpret_cast<void(*)()>(Test());
std::cout << !!foo << std::endl;
return 0;
}
What I expect as output 0 0 0, but the result 0 0 1(see demo ). Can someone explain why the boolean operator returns true when it contains a pointer to a null, non-invoked function? And please also mention the workaround for checking nullptrinstd::function
. , ( foo.target<void*>() == nullptr) , , , , ( ).