为什么“0f”不是“0f”?在 C++ 中被视为浮点文字?
为什么 0f
在 C++ 中不被视为浮点文字?
#include <iostream>
using namespace std;
int main(){
cout << 0f << endl;
return 0;
}
编译上面的内容给了我
C2509(语法错误:“号码后缀错误”)
。
Why isn't 0f
treated as a floating point literal in C++?
#include <iostream>
using namespace std;
int main(){
cout << 0f << endl;
return 0;
}
Compiling the above gives me
C2509 (syntax error: 'bad suffix on number')
using VS2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果这个设计决策有明确说明的原因,那么它将在 C99“基本原理”文档中(C++ 从 C 中逐字复制所有这些内容,而无需重新考虑)。但没有。这是关于“f”后缀的所有内容:
不过,有一个隐含的原因。请注意措辞:“添加了 ... 后缀以通过浮动常量传达类型信息。”该标准的作者认为,当您到达后缀时,数字常量已经明确地要么是整数,要么是浮点。后缀仅用于类别内的额外特殊性,它不能将数字从一个类别翻转到另一个类别。这得到了实际语法 (C99 §6.4.4) 的支持,其中首先将数字常量定义为整型常量或浮点常量,然后定义单独的类每个的后缀。
If there was an explicitly stated reason for this design decision, it would be in the C99 "Rationale" document (C++ copied all this stuff verbatim from C without reconsidering it). But there isn't. This is everything that's said about the 'f' suffix:
There is an implied reason, though. Note the wording: "the ... suffixes have been added to convey type information with floating constants." The authors of the standard were thinking of numeric constants as already being unambiguously either integer or floating point by the time you get to the suffix. The suffix is only for extra specificity within the category, it can't flip a number from one category to another. This is backed up by the actual grammar (C99 §6.4.4) which first defines numeric constants as being either integer-constants or floating-constants, and then defines separate classes of suffixes for each.
假设 C++ 用于浮点常量的语法与 C 相同(我认为这是正确的),我们有:
一些快捷方式的定义取自
ANSI C 语法
现在是
f
或F
您会看到浮点的末尾是在上面的 FS 中定义的。现在让我们看看识别有效浮点常量的语法:
现在,如果您仔细观察,没有任何规则可以识别
0f
。使用规则 1,我们可以得到
0e0f
使用规则 2,我们可以得到
.0f
或0.0f
使用规则 3,我们可以得到
0.f 或
0.0f
在您的情况下实际发生的是
0f
的0
将被词法分析器作为整数常量D
和f
将作为FS
令牌使用。现在,当解析发现D
后跟FS
且没有匹配规则时,它会输出错误:Assuming grammar used by C++ for floating point constants is same as that for C (which I think is true), we have:
Definitions of some shortcuts taken from
ANSI C grammar
Now the
f
orF
you see at the end of the floating points is in defined inFS
above.Now lets see the grammar to recognize valid floating point constants:
Now if you see carefully there is no rule that would identify
0f
.Using rule1 we can have
0e0f
Using rule2 we can have
.0f
or0.0f
Using rule3 we can have
0.f
or0.0f
What actually happen in your case is the
0
of0f
will be consumed by the lexical analyzer as an integer constantD
and thef
will be consumed as theFS
token. Now when the parse sees aD
followed byFS
for which there is no matching rule, it spits out the error:因为0是整数常量。
编辑:codepad.org 给出的错误消息(假设是 g++)可能更容易理解一点。 “错误:整数常量上的后缀“f”无效”。 “0.f”会起作用,因为 0. (或 0.0,同样的东西)是十进制常量,并且要求十进制常量为浮点比要求整数常量为浮点更有意义:)
Because the 0 is an integer constant.
edit: The error message given by codepad.org (assume g++) may be a little easier to understand. "error: invalid suffix "f" on integer constant". A "0.f" will work because 0. (or 0.0, same thing) is a decimal constant, and asking for a decimal constant to be a float makes more sense than asking for an integer constant to be a float :)
因为你需要
0.0f
。Because you need
0.0f
.这里有一个“因为”:如果带有
f
后缀的int
常量自动转换为float
,则0x0f 会产生歧义。
Here's a 'because' for you: if an
int
constant with anf
suffix was automatically converted tofloat
, then0x0f
would be ambiguous.这不一定是唯一的原因,但
l
或L
后缀可以应用于整数文字或浮点文字。42L
的类型为long int
;42.0L
的类型为long double
。带有
L
后缀的数字文字必须消除歧义才能确定它是整数还是浮点数。允许F
后缀本身来确定文字的类型将是不一致的,并且可能会造成混乱。这也将使在该语言的未来版本中添加新后缀变得更加困难。This isn't necessarily the only reason, but an
l
orL
suffix can be applied to an integer literal or to a floating-point literal.42L
is of typelong int
;42.0L
is of typelong double
.A numeric literal with an
L
suffix must be disambiguated to determine whether it's integer or floating-point. Allowing anF
suffix by itself to determine the type of a literal would be inconsistent and potentially confusing. It would also make it more difficult to add new suffixes in future versions of the language.