Why does the following code prints "0" as the output?
#include <functional>
#include <iostream>
int main()
{
typedef void (*fp_t)();
fp_t fp = nullptr;
std::function<void()> f = fp;
std::cout << (f == nullptr) << '\n';
}
I've tested it both with gcc 4.7.2 and MSVC-11.0.
I think that it's should print "1" because of the following quote from the standard:
ISO/IEC 14882:2011
20.8.11.2.1 function construct/copy/destroy [func.wrap.func.con]
template<class F> function(F f);template<class F, class A> function(allocator_arg_t, const A& a, F f);...
8 Postconditions:
!*thisif any of the following hold: —fis aNULLfunction pointer. —fis aNULLpointer to member. —Fis an instance of the function class template, and!f
!*thisdoes not imply thatthis == nullptris true. One usesoperator booland the other usesoperator==. - chrisnullptrcompares true if it's empty, and initializing it with a null function pointer makes it empty. - chrisf == nullptrreturns!f. I tend to believe this is a bug - Andy Prowlstd::function, it turns out they do. - chris