C(99)和C ++(11)之间不兼容的区别是什么? [英] What are the incompatible differences between C(99) and C++(11)?

查看:143
本文介绍了C(99)和C ++(11)之间不兼容的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是由回复的帖子触发的由Herb Sutter 解释,MS决定不支持/不制作C99编译器,而无论如何都要使用C ++(11)标准中的C(99)功能。

This question was triggered by replie(s) to a post by Herb Sutter where he explained MS's decision to not support/make a C99 compiler but just go with the C(99) features that are in the C++(11) standard anyway.

一个评论员回答


(...)C很重要,至少应引起一点注意。

(...) C is important and deserves at least a little bit of attention.

有很多现有代码是有效的C,但不是
有效的C ++。该代码不太可能被重写(...)

There is a LOT of existing code out there that is valid C but is not valid C++. That code is not likely to be rewritten (...)

由于我只在MS C ++中编程,所以我真的不知道纯 C很好,即我不了解我正在使用的C ++语言的哪些细节不在C(99)中,而且我几乎看不出某些C99代码无法按原样在C ++中工作的线索编译器。

Since I only program in MS C++, I really don't know "pure" C that well, i.e. I have no ready picture of what details of the C++-language I'm using are not in C(99) and I have little clues where some C99 code would not work as-is in a C++ compiler.

请注意,我了解C99仅 restrict 关键字,对我来说似乎具有非常狭窄的应用程序,并且关于可变长度数组(我不确定它们的广泛性或重要性)。

Note that I know about the C99 only restrict keyword which to me seems to have very narrow application and about variable-length-arrays (of which I'm not sure how widespread or important they are).

此外,我对是否有任何重要的语义差异或陷阱,即C(99)代码将在C ++(11)下进行编译,但是在C ++编译器中所做的事情与在C编译器中有所不同。

Also, I'm very interested whether there are any important semantic differences or gotchas, that is, C(99) code that will compile under C++(11) but do something differently with the C++ compiler than with the C compiler.

快速链接:答案中的外部资源:

Quick links: External resources from the answers:

  • Wikipedia page
  • David R. Tribble's comparison (from 2001)
  • C++11 standard: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3376.pdf (section C.1)

推荐答案

如果您从C和C ++的公共子集开始,有时称为clean C(不是完全C90),则必须考虑三种不兼容类型:

If you start from the common subset of C and C++, sometimes called clean C (which is not quite C90), you have to consider 3 types of incompatibilities:


  1. 使合法C非法C ++的其他C ++功能

例如C ++关键字,可以用作C中的标识符或C中的隐式转换,但需要在C ++中进行显式转换。

Examples for this are C++ keywords which can be used as identifiers in C or conversions which are implicit in C but require an explicit cast in C++.

这可能是Microsoft仍然完全提供C前端的主要原因:否则,必须重写不能作为C ++编译的旧代码。

This is probably the main reason why Microsoft still ships a C frontend at all: otherwise, legacy code that doesn't compile as C++ would have to be rewritten.

不属于C ++的其他C功能

在C ++被派生之后,C语言并没有停止发展。例如可变长度数组,指定的初始化程序和 restrict 。这些功能可能非常方便,但不是任何C ++标准的一部分,其中一些可能永远都不会使用。

The C language did not stop evolving after C++ was forked. Some examples are variable-length arrays, designated initializers and restrict. These features can be quite handy, but aren't part of any C++ standard, and some of them will probably never make it in.

功能

An example for this would be the linkage of const objects or inline functions.

这样的例子是 const的链接对象或内联函数。

A list of incompatibilities between C99 and C++98 can be found here (which has already been mentioned by Mat).

C99和C ++ 98之间的不兼容列表可在此处找到(Mat已经提到过)

While C++11 and C11 got closer on some fronts (variadic macros are now available in C++, variable-length arrays are now an optional C language feature), the list of incompatibilities has grown as well (eg generic selections in C and the auto type-specifier in C++).

尽管C ++ 11和C11在某些方面越来越紧密(可变宏数组现在在C ++中可用,可变长度数组现在是C语言的可选功能),不兼容列表也在增加(例如,C语言中的泛型选择和C ++语言中的 auto 类型说明符)。

As an aside, while Microsoft has taken some heat for the decision to abandon C (which is not a recent one), as far as I know no one in the open source community has actually taken steps to do something about it: It would be quite possible to provide many features of modern C via a C-to-C++ compiler, especially if you consider that some of them are trivial to implement. This is actually possible right now using Comeau C/C++, which does support C99.

但是,这并不是一个紧迫的问题:就我个人而言,我对使用它非常满意Windows上的GCC和Clang,以及MSVC的专有替代品,例如Pelles C或Intel的编译器。

However, it's not really a pressing issue: Personally, I'm quite comfortable with using GCC and Clang on Windows, and there are proprietary alternatives to MSVC as well, eg Pelles C or Intel's compiler.

这篇关于C(99)和C ++(11)之间不兼容的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆