【Effective C++】 Item 1 View C++ as a federation of language 中英自译

    当看完这一节,开始对C++有一点更深的了解。为什么C++这么难去学会,掌握?这里Scott Meyers讲到——View C++ as a federation of language(将C++视为一个语言联邦),看着这个标题我也是各种不懂的= =,但看下去,便会发现如何去理清C++的大方向了。

    In the beginning, C++ was just C with some object-oriented features tacked on. Even C++'s original name, "C with Class", reflected this simple heritage.

    在开始,C++仅仅是C 附加上一些面向对象的特征。甚至C++最初的名字是,“C with Class”,反映出这简单的遗产。

    As the language matured, it grew bolder and more adventurous, adopting ideas,features, and programming strategies different from those of C with Class. Exceptions required different approaches to structuring functions (see Item 29). Templates gave rise to new ways of thinking about design (see Item 41),  and the STL defined an approach to extensibility unlike any most people had ever seen.

    作为一门语言逐渐成熟起来,它会变得更大胆和更冒险,采纳那些不同于C with Class的想法、特征和编程策略。异常(Exception)需要不同于原本的方法去组织函数。模版(Tamplate)引起了新的方式去考虑设计,和STL定义了一种几乎绝大部分人曾未见过如此可扩展性的方法

    Today's C++ is a multiparadigm programming language, one supporting a combination of procedural, object-oriented, functional, generic, and meteaprogramming features. This power and flexibility make C++ a tool without equal, but can also cause some confusion. All the "proper usage" rules seem to have exceptions. How are we to make sense of such a language?

    现在的C++是一种多重泛型编程语言,支持面向过程,面向对象,面向函数,类和元编程等特征。这种强大和灵活性使得C++变成一种无以伦比的工具,但是,同样导致了一些困惑。所有“正确使用”规则似乎都有例外,我们要怎样才能搞懂这样一种语言?

    The easiest way is to view C++ not as a single language but as a federation of related languages. Within a particular sublanguage, the rules tend to be simple, straightforward, and easy to remmeber. When you move from one sublanguage to another, however, the rules may changes. To make sense of C++, you have to recognize its primary sublanguages. Fortunately, there are only four:

    最简单的方法是,不要将C++视为一种单一的语言,而是将其作为一个亲属语言的联邦。在一种特别的子语言内,规则会趋于简单,明确的和易记忆的。当你从一种子语言转到另外一种,规则会改变。为了搞懂C++,你必须识别它的次语言。幸运的是,这仅仅有四种。

  • C. Way down deep, C++ is still based on C. Blocks, statements, the preprocessor, built-in data type, arrays, pointers, etc., all come form C. In many cases, C++ offers approaches to problems that are superior to their C counterparts ( e.g., see Item 2 (alternatives to the preprocessor) and 13 (using object to manage resources)), but when you find yourself working with the C part of C++, the rules for effective programming reflect C's more limited scope:no templates, no exceptions, no overloading, etc.
  • 归根结底,C++ 依然是基于 C 的。blocks(模块),statements(语句),preprocessor(预处理器),built-in data types(内建数据类型),arrays(数组),pointers(指针)等等,全都来自于 C。在很多方面。C++ 提出了比相应的 C 版本更高级的解决问题的方法(例如,参见 Item 2(选择 preprocessor(预处理器))和 13(使用 objects(对象)管理 resources(资源))),但是,当你发现你自己工作在 C++ 的 C 部分时,effective programming(高效编程)的规则表现了 C 的诸多限制范围:没有 templates(模板),没有 exceptions(异常),没有 overloading(重载)等等。
  • Object-Oriented C++.This part of C++ is what C with Classes was all about: classes (including constructors and destructors), encapsulations, inheritance, polymorphism, virtual functions (dynamic binding), etc. This is the part of C++ to which the classic rules for object-oriented design most directly apply.
  • C++ 的这部分就是 C with Classes 涉及到的全部:classes(类)(包括构造函数和析构函数),encapsulation(封装),inheritance(继承),polymorphism(多态),virtual functions (dynamic binding)(虚拟函数(动态绑定))等。C++ 的这一部分直接适用于 object-oriented design(面向对象设计)的经典规则。
  • Template C++. This is the generic programming part of C++, the one that most programmers have the least experience with. Template considerations prevade C++, and it's not uncommon for rules of good programming to include special template-only clauses(e.g., see Item 46 on facilitating type conversions in calls to template functions). In fact, tempaltes are so powerful, they give rise to a completely new programming paradigm, template metaprogramming(TMP). Item 48 provides an overview of TMP, but unless you're a hard core template junkie, you need not worry about it. The rules for TMP rarely interact with mainstream C++ programming.
  • 这是 C++ 的 generic programming(泛型编程)部分,大多数程序员对此都缺乏经验。template(模板)的考虑已遍及 C++,而且好的编程规则中包含特殊的 template-only(模板专用)条款已不再不同寻常(参见 Item 46 通过调用 template functions(模板函数)简化 type conversions(类型转换))。实际上,templates(模板)极为强大,它提供了一种全新的 programming paradigm(编程范式)—— template metaprogramming (TMP) (模板元编程)。Item 48 提供了一个 TMP 的概述,但是,除非你是一个 hard-core template junkie(死心塌地的模板瘾君子),否则你不需在此费心,TMP 的规则对主流的 C++ 编程少有影响。
  • The STL. The STL is a template library, of course, but it's a very special template library. Its conventions regarding containers, iterators, algorithms, and function objects mesh beautifully, but templates and libraries can be built around other ideas, too. The STL has particular ways of doing things, and when you're working with the STL, you need to be sure to follow its conventions.
  • STL 是一个 template library(模板库),但它一个非常特殊的 template library(模板库)。它将 containers(容器),iterators(迭代器),algorithms(算法)和 function objects(函数对象)非常优雅地整合在一起,但是。templates(模板)和 libraries(库)也可以围绕其它的想法建立起来。STL 有很多独特的处事方法,当你和 STL 一起工作,你需要遵循它的规则。

    Keep these four sublanguages in mind, and don't be surprised when you encounter situations where effective programming requires that you change strategy when you switch from one sublanguage to another. For example, pass-by-value is generally more efficient than pass-by-reference for bulit-in (i.e., C-like) types, but when you move from the C part of C++ to Object-Oriented C++, the existence of user-defined constructors and destructors means that pass-by-reference-to-const is usually better. This is especially the case when working in Template C++, because there, you don't even know the type of object you're dealing with. When you cross into the STL, however, you know that iterators and funcion objects are modeled on pointers in C, so for iterators and function objects in the STL, the old C pass-by-value rule applies again. (For all the details on choosing among parameter-passing options, see Item 20.)

记住这四种子语言,还有不用感到意外当你从一种子语言到另外一种子语言,高效编程需要你改变策略的情况。例如,对于内置类型来说,pass-by-value(值传递)一般比pass-by-reference(引用传递)更有效率。但当你从C++的C部分转换到C++的面向对象部分,自定义的构造函数和析构函数意味着pass-by-reference-to-const(const引用传递)会更好。这特别在使用Template C++的使用,因为你甚至不可能知道你正在处理的对象的类型。当你转到STL时,然而,你要知道iterators(迭代器)和functions objects(函数对象)是以C语言的指针为蓝本的。所以旧的C语言的值传递规则再次适用于SLT中的iterators(迭代器)和functions objects(函数对象)。(参数传递选项之间进行选择的所有细节,请参阅Item 20)。

    C++, then, isn't a unified language with a single set of rules; it's a federation of four sublanguages, each with its own conventions. Keep these sublanguages in mind, and you'll find that C++is a lot easier to understand.

C++,不是一种单一的一套规则的统一标准的语言;它是一个四个子语言的联邦,每一个都有它自己的约定。将这些子语言记在心里,你会发现C++会更容易理解。

Things to Remember

  • Rules for effective C++ programming vary, depending on the part of C++ you are using.
  • 高效的C++编程规则有所不同,取决于你在使用的那一C++部分


未完待续……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值