C 与 C++ 之间的关系

发布于 2024-10-06 11:44:28 字数 221 浏览 0 评论 0原文

当 Stroustroup 设计 C++ 时,他的目标之一是 C++ 尽可能成为 C 的超集。我知道这不是 100% 的情况,但大多数好的 C 代码也是 C++ 代码。

但我听说 C99 支持许多 C++ 不支持的功能(如 VLA),甚至还有 C1x 或 C0x,无论它叫什么。那么,C++ 只是 C89 的超集,从那时起,C 和 C++ 就几乎独立发展了?

When Stroustroup was designing C++, one of his goals was that C++ were as much a superset of C as possible. I understand that this is not 100% the case, but most of good code in C is also C++ code.

But I am hearing that C99 supports many things that C++ doesn't (like VLA) and that there is even going to be C1x or C0x whatever it's called. So, C++ is a superset of only old C89 and from then on C and C++ are developing pretty much independently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

旧竹 2024-10-13 11:44:28

C++ 几乎是 C89/C90 的超集。 (不过,我不建议编写代码的目的是能够将其编译为 C 或 C++。)从那时起,C99 出现了分歧。新的 C++ 标准(通常称为 C++0x)将尝试提高兼容性,但不会包含 C99 可变长度数组之类的内容。 Stroustrup 对 C 委员会的一些行为表示失望,显然希望他们能够尽量接近 C++ 兼容性。

所以,是的,语言是有差异的。

C++ is a near superset of C89/C90. (I don't recommend writing the code with the purpose of being able to compile it as either C or C++, though.) Since then, C99 diverged. The new C++ standard (often called C++0x) will have some attempt to be more compatible, but isn't going to have such things as the C99 variable-length arrays. Stroustrup has expressed disappointment with some of the C committee's actions, apparently having expected them to try to keep closer to C++ compatibility.

So, yes, the languages are diverging.

巷子口的你 2024-10-13 11:44:28

这是正确的。 C++ 最初开发时是作为 C 的超集。从那时起,两人就各自独立了。

That is correct. C++ started off as a superset of C when it was originally developed. Since that time, the two have grown independent of the other.

澉约 2024-10-13 11:44:28

我们共同努力保持语言尽可能兼容,C++0x 将采用 C99 的一些更改。但它们似乎确实可能会出现某种程度的分歧,其中 VLA 是最显着的差异。我也不知道C++会采用restrict

There is a concerted effort to keep the languages as compatible as is practicable, and C++0x will adopt some of C99's changes. But it does seem likely that they will diverge to some extent, with VLAs being the most notable discrepancy. I don't know that C++ will adopt restrict either.

喵星人汪星人 2024-10-13 11:44:28

Bjarne Stroustrup 撰写的一篇有趣的文章阐明了该主题:兄弟之争:C 和 C++ (pdf)

An interesting article by Bjarne Stroustrup that sheds some light on the topic: Sibling Rivalry: C and C++ (pdf)

抹茶夏天i‖ 2024-10-13 11:44:28

但是大多数优秀的 C 代码也是 C++ 代码。

不。

我想说大多数优秀的 C 代码都可以使用 C++ 编译器进行编译。
这并不意味着它是 C++ 代码。

C++ 是旧 C89 的超集,从那时起 C 和 C++ 就几乎独立开发了?

C++ 基于 C89。
C 通过 C99 进行了扩展,其中很少一部分被合并到 C++03
对于 C++0x,我们正在不断努力最小化差异并使语言更接近(在合理的情况下)

but most of good code in C is also C++ code.

No.

I would say that most good C code is compilable using a C++ compiler.
That does not make it C++ code.

C++ is a superset of only old C89 and from then on C and C++ are developing pretty much independently?

C++ was based of C89.
C was extended with C99 very little of which was incorporated into C++03
There are ongoing efforts to minimize the difference and bring the languages closer (where it is reasonable) for C++0x

王权女流氓 2024-10-13 11:44:28

即使对于旧的 C,又名 C89,在幕后的差异也很难处理,从对于一个而不是另一个而言左值的运算符返回,对一个而不是另一个有效的控制流等等。它们可以的地方是 on带有原型、结构体等的函数的接口级别。

对于较新版本的语言,这种差异甚至更大,因为即使是接口兼容性也可能难以维护。 C99 已经有用于数组函数参数边界的 static 关键字,两种语言中编译时常量的概念有很大不同,C++ 开始重复使用旧关键字 (auto) 并过度使用使用不在保留命名空间中的新关键字...

所以,是的,我认为分歧将会扩大,对于两个社区来说,承认离婚并尝试分开相处可能会更好。

Even for old C, aka C89, under the hood the difference is difficult to handle, returns from operators that are lvalue for one and not the other, control flow that is valid for one and not the other etc. Where they are ok is on an interface level for functions with prototypes, struct etc.

For newer version of the languages this diverges even more, since even interface compatibility might be difficult to maintain. C99 already has the static keyword for bounds of array function parameters, the concepts of compile time constants are quite different in both languages, C++ starts reusing old keywords (auto) and excessively uses new keywords that are not in a reserved namespace...

So yes, I think the divide will grow, and it would probably better for both communities to admit the divorce and to try to get along separately.

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