是 c++一种无空间的语言?
#define PR ( A, B ) cout << ( A ) << ( B ) << endl ;
-错误-> A 未在范围内声明
- 错误-> B 未在范围内声明
- 错误-> “cout”之前应有“,”
我认为C++是无空间语言,但是当我编写上面的代码时,我看到了一些错误。 我仍在想“是我的控制台或库工作不正常吗?”。
如果我没记错的话,怎么会有人说“C++ 是一种无空间语言”?
#define PR ( A, B ) cout << ( A ) << ( B ) << endl ;
- error -> A was not declared in scope
- error -> B was not declared in scope
- error -> expected "," before "cout"
I thought C++ was space free language but when I write above code, then I see some errors.
I am still thinking "Is my console is not working properly or library?".
If I am not wrong, how can someone say "C++ is a space free language"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在许多例外情况下,空格都很重要。这是其中之一。由于
PR
后面有空格,预处理器如何知道(A,B)
是否是宏扩展或其参数的一部分?它没有,并且简单地假设无论它在哪里看到PR
,它都应该替换( A, B ) cout << (一)<< (B)<<结束;
。空格很重要的另一个地方是嵌套模板参数中,例如:
最后的空格是强制性的,否则编译器会假设它是
>>
运算符。 (尽管我相信这已经在 C++0x 中解决了)。另一个例子是:
出于显而易见的原因,两个
+
符号之间的空格是强制性的。There are numerous exceptions where whitespace matters; this is one of them. With the space after
PR
, how is the preprocessor supposed to know whether(A,B)
is part of the macro expansion, or its arguments? It doesn't, and simply assumes that wherever it seesPR
, it should substitute( A, B ) cout << ( A ) << ( B ) << endl ;
.Another place where whitespace matters is in nested template arguments, e.g.:
That final space is mandatory, otherwise the compiler assumes it's the
>>
operator. (Although I believe this is sorted out in C++0x).Yet another example is:
The space in between the two
+
symbols is mandatory, for obvious reasons.宏函数名称和参数列表开头的括号之间不能有空格。
换行符形式的空白也很重要,因为#define 语句在预处理器遇到换行符时结束。
请注意,在宏函数定义的末尾添加分号通常是一个坏主意,这会使它们在下面没有分号的情况下使用时看起来很混乱。
You can't have a space between the macro-function-name and the parenthesis starting the argument list.
Whitespace in the form of the newline also matters, because a #define statement ends when the preprocessor hits the newline.
Note that its usually a bad idea to put semicolons at the end of macro function definitions, it makes them look confusing when used without a semicolon below.
#define
不是 C++,它是预处理器。 C++ 的规则与预处理器的规则不同。要指示宏,名称和括号之间不能有空格。
A
#define
is not c++, it's preprocessor. The rules of c++ aren't the same as the rules of the preprocessor.To indicate a macro, you mustn't have a space between the name and the parenthesis.
你要求为我从未听过有人费心表达的主张进行辩护……?
预处理器阶段不遵循与后面的词法分析等阶段相同的规则。还有其他怪癖:在
>
关闭模板之间需要空格、换行符分隔注释、字符串文字无法嵌入实际换行符(与它们的转义序列不同)、字符内有空格以及字符串文字会影响它们……不过,与 Python 不同,以不同方式缩进和行分隔代码有很大的自由度。
You're asking for defense of a claim I've never heard anyone bother to voice...?
The preprocessor stage doesn't follow the same rules as the later lexing etc. stages. There are other quirks: the need for a space between
>
closing templates, newline-delimited comments, string literals can't embed actual newlines (as distinct from escape sequences for them), space inside character and string literals affects them....Still, there's a lot of freedom to indent and line-delimit the code in different ways, unlike in say Python.
您可以将 c++ 预处理器视为预处理器(编译器的一部分)的指令,而不完全是“c++ 空间”的一部分。因此,尽管两个“空间”之间共享许多引用,但规则确实不同。
You can think of the c++ preprocessor as instruction to the preprocessor (part of the compiler) and not exactly a part of the "c++ space".. So the rules are indeed different although many references are shared between the two 'spaces'..