为什么有人会使用 C 而不是 C++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/497786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would anybody use C over C++?
提问by
Although people seem to like to complainabout C++, I haven't been able to find much evidence as to why you would want to choose C over C++. C doesn't seem to get nearly as much flak and if C++ has all these problems why can't you just restrict yourself to the C subset? What are your thoughts/experience?
尽管人们似乎喜欢抱怨C++,但我无法找到太多证据来说明您为什么要选择 C 而不是 C++。C 似乎并没有受到那么多的抨击,如果 C++ 有所有这些问题,为什么你不能把自己限制在 C 子集上呢?你有什么想法/经验?
采纳答案by Adam Davis
Joel's answer is good for reasons you might haveto use C, though there are a few others:
Joel 的回答很好,因为您可能必须使用 C,但还有其他一些原因:
- You must meet industry guidelines, which are easier to prove and test for in C.
- You have tools to work with C, but not C++ (think not just about the compiler, but all the support tools, coverage, analysis, etc)
- Your target developers are C gurus
- You're writing drivers, kernels, or other low level code
- You know the C++ compiler isn't good at optimizing the kind of code you need to write
- Your app not only doesn't lend itself to be object oriented, but would be harder to write in that form
- 您必须符合行业指南,这些指南在 C 中更容易证明和测试。
- 您有使用 C 的工具,但没有 C++(不仅要考虑编译器,还要考虑所有支持工具、覆盖率、分析等)
- 您的目标开发人员是 C 专家
- 您正在编写驱动程序、内核或其他低级代码
- 您知道 C++ 编译器不擅长优化您需要编写的代码类型
- 您的应用程序不仅不适合面向对象,而且更难以这种形式编写
In some cases, though, you might wantto use C rather than C++:
但是,在某些情况下,您可能希望使用 C 而不是 C++:
- You want the performance of assembler without the trouble of coding in assembler (C++ is, in theory, capable of 'perfect' performance, but the compilers aren't as good at seeing optimizations a good C programmer will see)
- The software you're writing is trivial, or nearly so - whip out the tiny C compiler, write a few lines of code, compile and you're all set - no need to open a huge editor with helpers, no need to write practically empty and useless classes, deal with namespaces, etc. You can do nearly the same thing with a C++ compiler and simply use the C subset, but the C++ compiler is slower, even for tiny programs.
- You need extreme performance or small code size, and know the C++ compiler will actually make it harder to accomplish due to the size and performance of the libraries
- 你想要汇编器的性能而没有在汇编器中编码的麻烦(C++ 理论上能够提供“完美”的性能,但编译器并不擅长看到一个优秀的 C 程序员会看到的优化)
- 您正在编写的软件是微不足道的,或者几乎是微不足道的 - 拿出微型 C 编译器,编写几行代码,编译,然后一切就绪 - 无需打开带有助手的大型编辑器,无需实际编写空的和无用的类,处理命名空间等。你可以用 C++ 编译器做几乎相同的事情,只需使用 C 子集,但 C++ 编译器更慢,即使对于小程序也是如此。
- 您需要极高的性能或较小的代码大小,并且知道 C++ 编译器实际上会因库的大小和性能而使其更难完成
You contend that you could just use the C subset and compile with a C++ compiler, but you'll find that if you do that you'll get slightly different results depending on the compiler.
您争辩说您可以只使用 C 子集并使用 C++ 编译器进行编译,但是您会发现如果这样做,您将获得略有不同的结果,具体取决于编译器。
Regardless, if you're doing that, you're using C. Is your question really "Why don't C programmers use C++ compilers?" If it is, then you either don't understand the language differences, or you don't understand compiler theory.
无论如何,如果你这样做,你正在使用 C。你的问题真的是“为什么 C 程序员不使用 C++ 编译器?” 如果是,那么您要么不了解语言差异,要么不了解编译器理论。
回答by plan9assembler
I like minimalism & simplicity.
我喜欢极简主义和简单。
回答by Joel Coehoorn
- Because they already know C
- Because they're building an embedded app for a platform that only has a C compiler
- Because they're maintaining legacy software written in C
- You're writing something on the level of an operating system, a relational database engine, or a retail 3D video game engine.
- 因为他们已经知道 C
- 因为他们正在为只有 C 编译器的平台构建嵌入式应用程序
- 因为他们在维护用 C 编写的遗留软件
- 您正在编写操作系统、关系数据库引擎或零售 3D 视频游戏引擎级别的内容。
回答by Andrew Grant
Fears of performance or bloat are not good reason to forgo C++. Every language has its potential pitfalls and trade offs - good programmers learn about these and where necessary develop coping strategies, poor programmers will fall foul and blame the language.
担心性能或膨胀并不是放弃 C++ 的好理由。每种语言都有其潜在的陷阱和权衡——优秀的程序员了解这些并在必要时制定应对策略,糟糕的程序员会犯规并责怪语言。
Interpreted Python is in many ways considered to be a "slow" language, but for non-trivial tasks a skilled Python programmer can easily produce code that executes faster than that of an inexperienced C developer.
解释型 Python 在许多方面被认为是一种“慢”语言,但对于重要的任务,熟练的 Python 程序员可以轻松生成比没有经验的 C 开发人员执行得更快的代码。
In my industry, video games, we write high performance code in C++ by avoiding things such as RTTI, exceptions, or virtual-functions in inner loops. These can be extremely useful but have performance or bloat problems that it's desirable to avoid. If we were to go a step further and switch entirely to C we would gain little and lose the most useful constructs of C++.
在我的行业,视频游戏中,我们通过避免内部循环中的诸如 RTTI、异常或虚函数之类的事情,用 C++ 编写高性能代码。这些可能非常有用,但存在性能或膨胀问题,这是需要避免的。如果我们更进一步并完全切换到 C,我们将收获甚微,并失去 C++ 最有用的结构。
The biggest practical reason for preferring C is that support is more widespread than C++. There are many platforms, particularly embedded ones, that do not even have C++ compilers.
首选 C 的最大实际原因是支持比 C++ 更广泛。有许多平台,尤其是嵌入式平台,甚至没有 C++ 编译器。
There is also the matter of compatibility for vendors. While C has a stable and well-defined ABI (Application Binary Interface) C++ does not. The ABI in C++ is more complicated due to such things as vtables and constructurs/destructors so is implemented differently with every vendor, and even versions of a vendors toolchain.
还有供应商的兼容性问题。虽然 C 具有稳定且定义明确的 ABI(应用程序二进制接口),但 C++ 没有。由于 vtable 和构造函数/析构函数等因素,C++ 中的 ABI 更加复杂,因此每个供应商甚至供应商工具链的版本都以不同的方式实现。
In real-terms this means you cannot take a library generated by one compiler and link it with code or a library from another which creates a nightmare for distributed projects or middleware providers of binary libraries.
实际上,这意味着您不能将一个编译器生成的库与其他编译器的代码或库链接起来,这会为分布式项目或二进制库的中间件提供者带来噩梦。
回答by Andrew Grant
I choose to write in C because I enjoy working with a small, tight language. I like having access to a standard which can be read in a reasonable amount of time (for me -- I'm a very slow reader). Moreover, I use it to write software for embedded systems for which few desirable C++ compilers exist (like somePIC micro-controllers).
我选择用 C 编写,因为我喜欢使用一种小巧紧凑的语言。我喜欢访问可以在合理时间内阅读的标准(对我来说 - 我是一个非常慢的读者)。此外,我用它来为嵌入式系统编写软件,其中几乎没有理想的 C++ 编译器(如某些PIC 微控制器)。
回答by Dinah
I take the other view: why use C++ instead of C?
我采取另一种观点:为什么使用 C++ 而不是 C?
The book The C Programming Language(aka: K&R) tells you clearly how to do everything the language can do in under 300 pages. It's a masterpiece of minimalism. No C++ book even comes close.
The C Programming Language(又名:K&R)这本书在 300 页以内清楚地告诉您如何完成该语言可以做的所有事情。这是极简主义的杰作。甚至没有 C++ 书籍接近。
The obvious counterargument is that the same could be said of most, if not all, modern languages -- they also can't tell you how to do everything in only a few hundred pages. True. So why use C++ instead? Feature richness? Power? If you need something more feature rich or powerful then go with C#, Objective C, Java, or something else like that. Why burden yourself with the complexities of C++? If you need the degree of control C++ grants then I argue to use C. C can do anything and can do it well.
明显的反驳是,即使不是全部,大多数现代语言也可以这样说——它们也无法在仅仅几百页的内容中告诉您如何做所有事情。真的。那么为什么要使用 C++ 呢?功能丰富?力量?如果您需要功能更丰富或更强大的东西,请使用 C#、Objective C、Java 或其他类似的东西。为什么要让 C++ 的复杂性给自己带来负担?如果您需要 C++ 授予的控制程度,那么我认为使用 C。C 可以做任何事情并且可以做得很好。
回答by jdisk
In addition to several other points mentioned already:
除了已经提到的其他几点:
Less surprise
少惊喜
that is, it is much easier to see what a piece of code will do do exactly. In C++ you need to approach guru level to be able to know exactly what code the compiler generates (try a combination of templates, multiple inheritance, auto generated constructors, virtual functions and mix in a bit of namespace magic and argument dependent lookup).
也就是说,它是很容易看到的一段代码会做这样做究竟。在 C++ 中,您需要接近大师级别才能准确地知道编译器生成的代码(尝试组合模板、多重继承、自动生成的构造函数、虚函数并混合一些命名空间魔术和参数相关查找)。
In many cases this magic is nice, but for example in real-time systems it can really screw up your day.
在许多情况下,这种魔力很好,但例如在实时系统中,它真的会搞砸你的一天。
回答by Ric Tokyo
Linus' answer to your question is "Because C++ is a horrible language"
Linus 对你的问题的回答是“因为 C++ 是一种可怕的语言”
His evidence is anecdotal at best, but he has a point..
他的证据充其量只是轶事,但他有一个观点。
Being more of a low level language, you would prefer it to C++..C++ is C with added libraries and compiler support for extra features (both languages have features the other language doesn't, and implement things differently), but if you have the time and experience with C, you can benefit from extra added low level related powers...[Edited](because you get usedto doing more work manually rather than benefit from some powers coming from the language/compiler itself)
作为一种更底层的语言,你更喜欢它而不是 C++..C++ 是 C,增加了库和编译器支持额外的特性(两种语言都具有另一种语言没有的特性,并且实现方式不同),但如果你有使用 C 的时间和经验,您可以从额外添加的低级相关功能中受益... [已编辑](因为您习惯于手动完成更多工作,而不是从语言/编译器本身的一些功能中受益)
Adding links:
添加链接:
Why are you still using C? PDF
I would googlefor this.. because there are plenty of commentaries on the web already
我会用谷歌搜索这个……因为网上已经有很多评论了
回答by Michel
Because they're writing a plugin and C++ has no standard ABI.
因为他们正在编写一个插件,而 C++ 没有标准的 ABI。
回答by Frank
Long compile times can be annoying. With C++ you can have very long compile times (which means, of course, more time for Stack Overflow!).
编译时间长可能很烦人。使用 C++ 你可以有很长的编译时间(当然,这意味着更多的时间用于堆栈溢出!)。