Printing time function to console produces 1 [duplicate]

梦想与她 提交于 2019-12-13 20:36:40

问题


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.



来源:https://stackoverflow.com/questions/52478974/printing-time-function-to-console-produces-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!