-->

Printing time function to console produces 1 [dupl

2019-08-24 22:05发布

问题:

This question already has an answer here:

  • How to print function pointers with cout? 7 answers

A couple of developers and I were wondering why:

std::cout<<std::time<<std::endl;

prints out a value of 1. What does the value represents, and why the value is of 1.

Answer to what happened: How to print function pointers with cout?

The C++ Standard specifies:

4.12 Boolean conversions

1 An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool.

Quote from anon:

This is the only conversion specified for function pointers.

Edit: The answer below nicely presents the solution as to why 1 was printed rather than just any bool and explained when 1 would not occur.

回答1:

The cppreference says that:

There are no overload for pointers to non-static member, pointers to volatile, or function pointers (other than the ones with signatures accepted by the (10-12) overloads). Attempting to output such objects invokes implicit conversion to bool, and, for any non-null pointer value, the value 1 is printed (unless boolalpha was set, in which case true is printed).

So you get function pointer std::time converted to bool and it is always true that is without boolalpha set output as 1.