向控制台打印时间函数产生 1

Printing time function to console produces 1

几个开发人员和我想知道为什么:

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

打印出值为 1。该值代表什么,为什么值为 1。

对发生的事情的回答: How to print function pointers with cout?

C++ 标准规定:

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.

引自匿名:

This is the only conversion specified for function pointers.

编辑:下面的答案很好地展示了为什么打印 1 而不是任何布尔值的解决方案,并解释了 1 何时不会出现。

cppreference 说:

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).

所以你得到函数指针 std::time 转换为 bool 并且它总是 true 没有 boolalpha 将输出设置为 1.