- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
C++ IO 流的基类 std::basic_ios
定义了 operator void*()
以返回 !fail()
和 operator!()
返回 fail()
。这让我想知道为什么我们根本需要 operator!()
。当然,!is
也可以通过隐式调用 operator void*()
并取反其结果来工作。
我在这里遗漏了什么,还是纯粹出于历史原因定义了 std::basic_ios::operator!()
?
A question on comp.lang.c++.moderated也没有带来任何答案。
最佳答案
使用旧的(阅读:cfront
后不久)C++ 编译器,编译器不能保证在需要时隐式调用对象的类型转换运算符。如果 iostream
没有声明 operator !
,那么您就不能期望 !cout
在所有情况下都能正常工作。 C++89(或 C++98 之前的标准的任何名称)只是留下未定义的区域。
这也是 operator void*()
被重载的原因,而不是 operator int
或 operator bool
的原因。 (bool
当时在标准中甚至不作为其自身的类型存在。)我记得我的教授告诉我 if()
在幕后期望一个void*
在 C++ 中,因为该类型可以充当相对于将传递给 if
语句的那些表达式结果类型的“超集”类型,但我还没有找到这在任何地方都有说明。
当时是 gcc 2 左右,当时大多数人不支持模板或异常,或者即使支持,也没有完全支持它们,因此使用模板进行元编程 C++ 仍然是一种理论练习,您必须确保检查 operator new
是否返回空指针。
这让我抓狂了好几年。
Stroustrup 的 The C++ Programming Language 的有趣摘录,第 3 版。 (1997),第 276 页:
The istream and ostream types rely on a conversion function to enable statements such as
while (cin >> x) cout << x;
The input operation cin>>x returns an istream&. That value is implicitly converted to a value indicating the state of cin. The value can then be tested by while. However, it is typically not a good idea to define an implicit conversion from one type to another in such a way that information is lost in the conversion.
在 C++ 中有很多似乎是可爱或聪明胜过一致的胜利。如果 C++ 足够聪明,可以将上述循环处理为:
while (!(cin >> x).fail()) cout << x;
因为这虽然更冗长且标点符号更多,但对于初学者来说更清晰。
...实际上,仔细想想,我不喜欢这两种结构。拼写出来:
for(;;)
{ cin >> x;
if(!cin)
break;
cout << x;
}
为什么我更喜欢这个?因为这个版本让如何扩展代码变得更加清晰,比如一次处理两次读取而不是一次读取。例如,“现有代码复制了一系列浮点值。我们希望您对其进行更改,以便将浮点值配对并写出,每行两个,因为我们现在使用的是复数。”
但我离题了。
关于c++ - 为什么 std::basic_ios 会重载一元逻辑否定运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3222131/
Or 运算符 对两个表达式进行逻辑“或”运算。 result = expression1 Or expression2 参数 result 任意数值变量。 expression1 任意
Not 运算符 对表达式执行逻辑非运算。 result = Not expression 参数 result 任意数值变量。 expression 任意表达式。 说明 下表显示如何
Is 运算符 比较两个对象引用变量。 result = object1 Is object2 参数 result 任意数值变量。 object1 任意对象名。 object2 任意
\ 运算符 两个数相除并返回以整数形式表示的结果。 result = number1\number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
And 运算符 对两个表达式进行逻辑“与”运算。 result = expression1 And expression2 参数 result 任意数值变量。 expression1
运算符(+) 计算两个数之和。 result = expression1 + expression2 参数 result 任意数值变量。 expression1 任意表达式。 exp
我对此感到困惑snippet : var n1 = 5-"4"; var n2 = 5+"4"; alert(n1); alert(n2); 我知道 n1 是 1。那是因为减号运算符会将字符串“4”转
我想我会得到 12,而不是 7。 w++,那么w就是4,也就是100,而w++, w 将是 8,1000;所以 w++|z++ 将是 100|1000 = 1100 将是 12。 我怎么了? int
Xor 运算符 对两个表达式进行逻辑“异或”运算。 result = expression1 Xor expression2 参数 result 任意数值变量。 expression1
Mod 运算符 两个数值相除并返回其余数。 result = number1 Mod number2 参数 result 任意数值变量。 number1 任意数值表达式。 numbe
Imp 运算符 对两个表达式进行逻辑蕴涵运算。 result = expression1 Imp expression2 参数 result 任意数值变量。 expression1 任
Eqv 运算符 执行两个表达式的逻辑等价运算。 result = expression1 Eqv expression2 参数 result 任意数值变量。 expression1 任
我有一个运算符重载的简单数学 vector 类。我想为我的运算符(operator)获取一些计时结果。我可以通过计时以下代码轻松计时我的 +=、-=、*= 和/=: Vector sum; for(s
我是用户定义比较运算符的新手。我正在读一本书,其中提到了以下示例: struct P { int x, y; bool operator、运算符<等),我们
在 SQL 的维基百科页面上,有一些关于 SQL 中 bool 逻辑的真值表。 [1] 维基百科页面似乎来源于 SQL:2003 标准。 等号运算符 (=) 的真值表与 SQL:2003 草案中的 I
我遇到了一个奇怪的 C++ 运算符。 http://www.terralib.org/html/v410/classoracle_1_1occi_1_1_number.html#a0f2780081f
我正在阅读关于 SO 和 answers 中的一个问题,它被提到为: If no unambiguous matching deallocation function can be found, pr
我偶然发现了这个解决方案,但我无法理解其中到底发生了什么。谁能解释一下! 据我了解,它试图通过计算一半的单元格然后将其加倍来计算 a*b 网格中的单元格数量。但是我无法理解递归调用。 请不要建议其他解
Go的基本类型 布尔类型bool 长度:1字节 取值:布尔类型的取值只能是true或者false,不能用数字来表示 整型 通用整型 int / uint(有符号 / 无符号,下面也类似) 长度:根据运
在本教程中,您将学习JavaScript中可用的不同运算符,以及在示例的帮助下如何使用它们。 什么是运算符? 在JavaScript中,运算符是一种特殊符号,用于对运算数(值和变量)执行操作。例如,
我是一名优秀的程序员,十分优秀!