控制台打印时间功能产生1 [英] Printing time function to console produces 1
问题描述
几个开发人员,我想知道为什么:
A couple of developers and I were wondering why:
std::cout<<std::time<<std::endl;
打印出一个值1.该值代表什么,以及为什么值是1.
prints out a value of 1. What does the value represents, and why the value is of 1.
答案: 如何使用cout打印函数指针?
C ++标准指定:
4.12布尔转换
4.12 Boolean conversions
1算术,枚举,指针或成员指针的右值 可以将类型转换为bool类型的右值.
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而不是仅打印任何bool的解决方案,并解释了何时不会出现1.
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.
推荐答案
cppreference 表示:
指向非静态成员的指针没有指向的重载. volatile或函数指针(带签名的指针除外) (10-12)重载所接受).尝试输出此类对象 调用隐式转换为bool,并且对于任何非null指针 值,则打印值1(除非设置了boolalpha,在这种情况下 true会打印出来.)
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
.
So you get function pointer std::time
converted to bool
and it is always true
that is without boolalpha
set output as 1
.
这篇关于控制台打印时间功能产生1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!