是 c++一种无空间的语言?

发布于 2024-10-14 16:26:48 字数 315 浏览 2 评论 0原文

 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可可 2024-10-21 16:26:48

在许多例外情况下,空格都很重要。这是其中之一。由于 PR 后面有空格,预处理器如何知道 (A,B) 是否是宏扩展或其参数的一部分?它没有,并且简单地假设无论它在哪里看到 PR,它都应该替换 ( A, B ) cout << (一)<< (B)<<结束;

空格很重要的另一个地方是嵌套模板参数中,例如:

std::vector<std::vector<int> >

最后的空格是强制性的,否则编译器会假设它是 >> 运算符。 (尽管我相信这已经在 C++0x 中解决了)。

另一个例子是:

a + +b;

出于显而易见的原因,两个 + 符号之间的空格是强制性的。

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 sees PR, it should substitute ( A, B ) cout << ( A ) << ( B ) << endl ;.

Another place where whitespace matters is in nested template arguments, e.g.:

std::vector<std::vector<int> >

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:

a + +b;

The space in between the two + symbols is mandatory, for obvious reasons.

岁吢 2024-10-21 16:26:48

宏函数名称和参数列表开头的括号之间不能有空格。

#define PR(A, B) cout << ( A ) << ( B ) << endl

换行符形式的空白也很重要,因为#define 语句在预处理器遇到换行符时结束。

请注意,在宏函数定义的末尾添加分号通常是一个坏主意,这会使它们在下面没有分号的情况下使用时看起来很混乱。

You can't have a space between the macro-function-name and the parenthesis starting the argument list.

#define PR(A, B) cout << ( A ) << ( B ) << endl

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.

暗恋未遂 2024-10-21 16:26:48

#define 不是 C++,它是预处理器。 C++ 的规则与预处理器的规则不同。

要指示宏,名称和括号之间不能有空格。

#define PR(A, B)  cout << ( A ) << ( B ) << endl;

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.

#define PR(A, B)  cout << ( A ) << ( B ) << endl;
仲春光 2024-10-21 16:26:48

你要求为我从未听过有人费心表达的主张进行辩护……?

预处理器阶段不遵循与后面的词法分析等阶段相同的规则。还有其他怪癖:在 > 关闭模板之间需要空格、换行符分隔注释、字符串文字无法嵌入实际换行符(与它们的转义序列不同)、字符内有空格以及字符串文字会影响它们……

不过,与 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.

冬天旳寂寞 2024-10-21 16:26:48

您可以将 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'..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文