简体   繁体   English

C和C ++之间的关系

[英]Relationship between C and C++

When Stroustroup was designing C++, one of his goals was that C++ were as much a superset of C as possible. 当Stroustroup设计C ++时,他的目标之一是C ++尽可能多地是C的超集。 I understand that this is not 100% the case, but most of good code in C is also C++ code. 我了解并不是100%如此,但是C中的大多数好代码也是C ++代码。

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. 但是我听说C99支持许多C ++不支持的功能(例如VLA),甚至还有所谓的C1x或C0x。 So, C++ is a superset of only old C89 and from then on C and C++ are developing pretty much independently? 因此,C ++是仅 C89的超集,从那时起C和C ++几乎是独立开发的?

C++ is a near superset of C89/C90. C ++是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. (不过,我不建议编写代码以便能够将其编译为C或C ++。)从那时起,C99出现了分歧。 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. 新的C ++标准(通常称为C ++ 0x)将进行一些尝试以使其更加兼容,但不会像C99可变长度数组那样。 Stroustrup has expressed disappointment with some of the C committee's actions, apparently having expected them to try to keep closer to C++ compatibility. Stroustrup对某些C委员会的行动表示失望,显然是希望他们努力与C ++兼容。

So, yes, the languages are diverging. 因此,是的,语言在不断变化。

That is correct. 那是正确的。 C++ started off as a superset of C when it was originally developed. C ++最初是C语言的超集。 Since that time, the two have grown independent of the other. 从那时起,两者就相互独立了。

There is a concerted effort to keep the languages as compatible as is practicable, and C++0x will adopt some of C99's changes. 尽一切努力使这些语言在实际可行的情况下保持兼容,并且C ++ 0x将采用C99的某些更改。 But it does seem likely that they will diverge to some extent, with VLAs being the most notable discrepancy. 但是,它们之间似乎有一定的分歧,其中最明显的差异就是VLA。 I don't know that C++ will adopt restrict either. 我不知道C ++也会采用restrict

Bjarne Stroustrup的一篇有趣的文章阐明了以下主题: 同级竞争:C和C ++ (pdf)

but most of good code in C is also C++ code. 但是C语言中的大多数好代码也是C ++代码。

No. 没有。

I would say that most good C code is compilable using a C++ compiler. 我会说大多数好的C代码都可以使用C ++编译器进行编译。
That does not make it C++ code. 但这并不能成为C ++代码。

C++ is a superset of only old C89 and from then on C and C++ are developing pretty much independently? C ++是仅旧C89的超集,从那时起C和C ++几乎是独立开发的?

C++ was based of C89. C ++基于C89。
C was extended with C99 very little of which was incorporated into C++03 C随C99扩展,几乎没有合并到C ++ 03中
There are ongoing efforts to minimize the difference and bring the languages closer (where it is reasonable) for C++0x 我们一直在努力使差异最小化,并使C ++ 0x的语言更接近(在合理的范围内)

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. 即使对于旧的C(又名C89),在引擎盖下,这种差异也难以处理,从一个运算符返回一个值而不是另一个运算符的返回值,对一个有效值而不对另一个有效的控制流等。具有原型, struct等功能的接口级别

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... C99已经具有用于数组函数参数范围的static关键字,两种语言中的编译时间常数的概念都非常不同,C ++开始重用旧关键字( auto )并过度使用不在保留名称空间中的新关键字...

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. 因此,是的,我认为这种鸿沟将会加剧,两个社区承认离婚并尝试分开相处可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM