开发者

Relationship between C and C++

开发者 https://www.devze.com 2023-01-29 06:11 出处:网络
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 10开发者_运维问答0% the case, but most of good code in C is al

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 10开发者_运维问答0% 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?


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.


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.


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消