masterminds of programming 翻译 (六)

Would you recommend C++ for some systems where practitioners are reluctant to use it, such as system software and embedded applications?

在一些程序员不情愿使用C++的场合,例如系统软件或是嵌入式应用程序,你会推荐使用C++吗?

 

Bjarne: Certainly, I do recommend it and not everybody is reluctant. In fact, I don’t see much reluctance in those areas beyond the natural reluctance to try something new in established organizations. Rather, I see steady and significant growth in C++ use. For example, I helped write the coding guidelines for the mission-critical software for Lockheed Martin’s Joint Strike Fighter. That’s an “all C++ plane.” You may not be particularly keen on military planes, but there is nothing particularly military about the way C++ is used and well over 100,000 copies of the JSF++ coding rules have been downloaded from my home pages in less than a year, mostly by nonmilitary embedded systems developers, as far as I can tell.

当然了,我确实推荐它而且也不是每个人都不情愿。事实上,我除了在一些已经建立的组织看到一些尝试新事物时的一贯勉强外并没有看到那么多不情愿。恰恰相反,我看见C++的使用既显著又稳步地在增长。例如,我帮助编写了Lockheed Martin’s Joint Strike Fighter[1]使用的关键任务软件的代码纲领。那是一架“彻底的C++飞机”。你可能并不热心于军事飞机,但是在一年之内有超过100000份JSF++编码规则从我的主页上被下载和C++被使用的方式就和军事特殊性没啥关系了,我所知道的是他们大多是非军事嵌入式系统的开发者。

 

C++ has been used for embedded systems since 1984, many useful gadgets have been programmed in C++, and its use appears to be rapidly increasing. Examples are mobile phones using Symbian or Motorola, the iPods, and GPS systems. I particularly like the use of C++ on the Mars rovers: the scene analysis and autonomous driving subsystems, much of the earth-based communication systems, and the image processing.

C++自1984年以来就被用来开发嵌入式系统,许多有用的小配件就是用C++编的,而且它的使用出现快速增长势头。使用塞班和摩托罗拉系统的手机、ipods和GPS系统就是例子。我尤其喜欢C++在火星漫游者上的使用:图像分析和自动驾驶子系统,相当多的地表通信系统和图像处理。

 

People who are convinced that C is necessarily more efficient than C++ might like to have a look at my paper entitled “Learning Standard C++ as a New Language” [C/C++ Users Journal, May 1999], which describes a bit of design philosophy and shows the result of a few simple experiments. Also, the ISO C++ standards committee issued a technical report on performance that addresses a lot of issues and myths relating to the use of C++ where performance matters (you can find it online searching for “Technical Report on C++ Performance”).* In particular, that report addresses embedded systems issues.

那些深信C一定比C++更高效的人可能应该去看看我那篇名为《像学习一门新语言一样学习C++》[C/C++ Users Journal, May 1999],文章里描述了一些设计哲学以及展示了一些简单实验的结果。另外,ISO C++标准委员会出版了一份关于C++性能的技术报告,里面阐述了很多有关在需要高性能的地方使用C++的问题和迷思(你可以以“Technical Report on C++ Performance”作为关键字在网上找到它)。值得一提的是在那份报告里就说了关于嵌入式系统的问题。

 

Kernels like Linux’s or BSD’s are still written in C. Why haven’t they moved to C++? Is it something in the OO paradigm?

LinuxBSD的内核都还是用C编写的。为什么它们不转用C++?是因为C++是面向对象编程范式的缘故吗?

 

Bjarne: It’s mostly conservatism and inertia. In addition, GCC was slow to mature. Some people in the C community seem to maintain an almost willful ignorance based on decade-old experiences. Other operating systems and much systems programming and even hard real-time and safety-critical code has been written in C++ for decades. Consider some examples: Symbian, IBM’s OS/400 and K42, BeOS, and parts of Windows. In general, there is a lot of open source C++ (e.g., KDE).

这主要是保守主义和惯性造成的。另外,GCC过去成熟的就很慢。一些在C社区中的人们好像以很老旧的经验保持着一种故意的忽略。其它一些操作系统和很多系统编程甚至是困难的对实时性和安全性要求很高的代码都用C++写了很多年了。考虑一些例子:塞班,IBM’s OS/400,K42,BeOS和部分Windows。一般来说还有许多开源的C++程序(如,KDE)。

 

You seem to equate C++ use with OO. C++ is not and was never meant to be just an object-oriented programming language. I wrote a paper entitled “Why C++ is not just an Object-Oriented Programming Language” in 1995; it is available online.* The idea was and is to support multiple programming styles (“paradigms,” if you feel like using long words) and their combinations. The most relevant other paradigm in the context of high-performance and close-to-the-hardware use is generic programming (sometimes abbreviated to GP). The ISO C++ standard library is itself more heavily GP than OO through its framework for algorithms and containers (the STL). Generic programming in the typical C++ style relying heavily on templates is widely used where you need both abstraction and performance.

你好像把面向对象和C++等同起来看待。C++不是也从来不只是一门面向对象语言。我在1995年写了一篇名为“Why C++ is not just an Object-Oriented Programming Language”;现在网上还有。观点就是支持多种编程类型(“范式”,如果你喜欢长单词)和它们的组合。在高性能和接近硬件两方面最接近的另外一种范式是泛型编程(有时缩写为GP)。ISO C++标准库本身通过它的算法框架和容器(STL)比起面向对象范式来说更是泛型编程范式。非常倚赖模板的C++泛型编程在人们同时需要抽象和性能表现时被广泛应用。

 

I have never seen a program that could be written better in C than in C++. I don’t think such a program could exist. If nothing else, you can write C++ in a style close to that of C. There is nothing that requires you to go hog-wild with exceptions, class hierarchies, or templates. A good programmer uses the more advanced features where they help more directly to express ideas and do so without avoidable overheads.

我从来没见过哪个程序如果用C写会比用C++写更好。我不认为存在这样的程序。如果没有特别需要,你可以用接近C的风格来写C++。没有什么东西需要你疯狂的使用异常、类继承或者模板。一个好的程序员会在高级特性可以更直接帮助他们表达观点和使用它们没有什么可避免的额外开销的情况下使用它们。

 

Why should a programmer move his code from C to C++? What advantages would he have using C++ as a generic programming language?

Bjarne: You seem to assume that code first was written in C and that the programmer started out as a C programmer. For many—probably most—C++ programs and C++ programmers, that has not been the case for quite a while. Unfortunately, the “C first” approach lingers in many curricula, but it is no longer something to take for granted.

为什么程序员应该把他C代码转成C++代码?如果他把C++作为一门泛型编程语言使用可以得到哪些优势?

Bjarne:你好像假定代码首先是用C写成的以及一个程序员是作为一个C程序员开始其职业生涯的。可能对于大部分C++程序和C++程序员来说,事情并不是这样。不幸的是,“以C为先”的方法一直徘徊在课堂里,但现在它已经不是什么理所当然的事了。

 

Someone might switch from C to C++ because they found C++’s support for the styles of programming usually done with C is better than C’s. The C++ type checking is stricter (you can’t forget to declare a function or its argument types) and there is type-safe notational support for many common operations, such as object creation (including initialization) and constants. I have seen people do that and be very happy with the problems they left behind. Usually, that’s done in combination with the adoption of some C++ libraries that may or may not be considered object-oriented, such as the standard vector, a GUI library, or some application-specific library.

一些人可能因为他们发现C++对于C风格编程的支持比C的要好而从C转到C++。C++的类型检查更严格(你不能忘记申明一个函数或者一个参数的类型),C++里对于许多常用操作也有类型-安全的标注支持,如类的创建(包括初始化)和常数。我已经看过有人这么做且非常开心可以解决这些问题。通常,这些是以采用一些可能是也可能不是面向对象的C++的标准库的组合得以完成的,比如标准向量(vector)库,GUI库,或者一些特定应用的库。

 

Just using a simple user-defined type, such as vector, string, or complex, does not require a paradigm shift. People can—if they so choose—use those just like the built-in types. Is someone using std::vector “using OO”? I would say no. Is someone using a C++ GUI without actually adding new functionality “using OO”? I’m inclined to say yes, because their use typically requires the users to understand and use inheritance.

只是用一个简单的用户自定义类,如向量类,字符串类或是复数类并不需要有范式上的变换。人们可以像使用内置类型一样使用它们。用了std::vector的人就是用了面向对象编程范式?我觉得不是。用了C++GUI而事实上没有加入新的功能的人就使用了面向对象编程范式?我倾向于说是,因为它们的使用特别需要用户理解和使用继承。

 

Using C++ as “a generic-programming programming language” gives you the standard containers and algorithms right out of box (as part of the standard library). That is major leverage in many applications and a major step up in abstraction from C. Beyond that, people can start to benefit from libraries, such as Boost, and start to appreciate some of the functional programming techniques inherent in generic programming.

把C++当做泛型编程语言来使用你将很容易得到标准容器和算法(作为标准库的一部分)。那是在很多应用的一个主要杠杆以及从C中抽象时的主要步骤。除此以外,人们能够开始从诸如Boost的库中获益,同时也开始欣赏泛型编程与生俱来的一些功能性编程技术。

 

However, I think the question is slightly misleading. I don’t want to represent C++ as “an OO language” or “a GP language”; rather, it is a language supporting:

然而,我认为这个问题还有点误导性。我不希望把C++只是表示成“一个面向对象语言”或“一个泛型编程语言”,我希望它是一门支持:

 

• C-style programming

• Data abstraction

• Object-oriented programming

• Generic programming

• C风格编程

• 数据抽象

• 面向对象编程

• 泛型编程

 

Crucially, it supports programming styles that combines those (“multiparadigm programming” if you must) and does so with a bias toward systems programming.

关键的一点,它支持这些编程范式的组合,它在系统编程方面基本也是如此。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值