Automatic conversion from double/int/string to bool in C++(在 C++ 中从 double/int/string 自动转换为 bool)
问题描述
我是一名 Java 程序员,一直在尝试学习一点 C++ 以扩展我的知识.这是我认为由于隐式转换而起作用的一个小代码片段,但我想知道它引用了规范的哪一部分,以及什么时候我必须注意的其他规则它涉及隐式转换.是否有制定隐式转换规则的文档/链接/站点?
I'm a Java programmer who has been trying to learn a bit of C++ on the side to expand on my knowledge. Here is a small code snippet which I think works due to implicit conversion but I'd like to know which part of the specification does it refer to and what are the other rules which I must be aware of when it comes to implicit conversion. Is there a document/link/site out there which lays down the implicit conversion rules?
#include <vector>
#include <iostream>
#include <iterator>
int main(void) {
using namespace std;
vector<bool> a;
a.push_back("asdf");
a.push_back("");
a.push_back(12);
a.push_back(0.0);
copy(a.begin(), a.end(), ostream_iterator<bool>(cout, "
"));
return 0;
}
/*
output:
1
1
1
0
*/
TIA,
佐助
推荐答案
指针和整数以及布尔值都是整数类型.前三个都是指针或整数,因为它们都是非零的,所以它们转换为布尔值 true
.double
类型的第四个值转换为零整数值,因此 false
.
Pointers and integers, and also booleans, are integral types. The first three are all either pointers or integers, and since they are all non-zero, they convert to the boolean value true
. The fourth value of type double
converts to a zero integral value and hence false
.
无法表示为整数值(如无穷大和 NaN)的双精度数的转换未定义.
Conversion of doubles that are not representable as integral values (like infinity and NaN) is undefined.
有关详细信息,请参阅 4.9,有关布尔转换",请参阅 4.12:
See 4.9 for details, and also 4.12 for "Boolean conversions":
算术纯右值、无范围枚举、指针或指向成员类型的指针可以转换为bool 类型的纯右值.将零值、空指针值或空成员指针值转换为 false;任何其他值都将转换为 true.
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
你的 0.0
是零值的算术类型.
Your 0.0
is an arithmetic type of zero value.
也许你可能不熟悉 C++ 中的字符串字面量:""
表示数组 char[1] { 0 }
,而这个数组(一个元素)衰减为指向其第一个元素的指针,该元素必然是非空指针.类似地,asdf"表示一个数组char[5] { 'a', 's', 'd', 'f', 0 }
,这又一次衰减为a(非空)指向其第一个元素的指针.角色的实际价值完全无关紧要.
Perhaps you may not be familiar with string literals in C++: ""
denotes the array char[1] { 0 }
, and this array (of one element) decays to a pointer to its first element, which is necessarily a non-null pointer. Similarly, "asdf" denotes an array char[5] { 'a', 's', 'd', 'f', 0 }
, and again this decays to a (non-null) pointer to its first element. The actual value of the characters is entirely immaterial.
这篇关于在 C++ 中从 double/int/string 自动转换为 bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中从 double/int/string 自动转换为 bool


- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- C++ 协变模板 2021-01-01
- 静态初始化顺序失败 2022-01-01