C++语言的设计理念

节选自C++语言之父 Bjarne Stroustrup 撰写的C++发展史三部曲之二 Evolving a language in and for the real world: C++ 1991-2006。其中一段关于C++语言本身设计理念规则的描述笔者认为非常有启发,分享给大家。有兴趣的读者非常建议阅读这三篇文章的原文。

During my early years at Bell Labs, these ideals grew into a set of “rules of thumb” for the design of C++.

我早年在贝尔实验室工作期间,这些理念逐渐发展为C++设计的一套“经验法则”。

  • General rules:
    总体规则:

    • C++’s evolution must be driven by real problems.
      C++的演化必须由实际问题驱动。

    • Don’t get involved in a sterile quest for perfection.
      不要受困于对完美的无趣追求。

    • C++ must be useful now.
      C++必须现在就是实用的。

    • Every feature must have a reasonably obvious implementation.
      每个特性必须有一种合理程度下显而易见的实现方式。

    • Always provide a transition path.
      总是提供一种过渡路径。(注:这里主要是说从C到C++的过渡,例如,std::sort 就设计为既可以用在C++的迭代器上,也可以用在C的指针上)

    • C++ is a language, not a complete system.
      C++是一种语言,而不是一个完整的系统。(个人理解:不应将太多的功能作为语言级的特性,可以通过库提供)

    • Provide comprehensive support for each supported style.
      为每种风格提供全面的支持。(注:Effective C++ 一书中 Item 1 就有提到:C++可以视为四个子语言的联邦:过程性编程(类似C)、面向对象编程(OOP)、泛型编程(GP)和 标准模板库 STL。C++不断推出的新特性也有其主要服务的目标。)

    • Don’t try to force people to use a specific programming style.
      不要试图强迫人们使用某种特定的编程风格。

  • Design support rules:
    设计支持上的规则:

    • Support sound design notions.
      支持健全的设计理念

    • Provide facilities for program organization.
      为程序的组织管理提供工具。

    • Say what you mean.
      所述即所得。换言之,你想要的可以通过直接表述对应的意思实现(不要采用晦涩的设计)。

    • All features must be affordable.
      所有特性必须是可以负担的。

    • It is more important to allow a useful feature than to prevent every misuse.
      允许使用一个实用的特性比避免所有误用更重要。(不要因小失大)

    • Support composition of software from separately developed parts.
      支持分别开发的部件能组合成一个完整软件。

  • Language-technical rules:
    语言技术上的规则:

    • No implicit violations of the static type system.
      不允许隐式地违背对静态类型系统。(注:C++是一种强类型的语言,而且类型系统是静态的(换言之,是编译期检查的),这是C++能在运行时保持高效率的一个重要因素。)

    • Provide as good support for user-defined types as for built-in types.
      对用户定义的类型提供和内建类型同等的支持。

    • Locality is good.
      局部性是好文明。

    • Avoid order dependencies.
      避免对顺序的依赖。

    • If in doubt, pick the variant of a feature that is easiest to teach.
      如有不确定,选择某个特性的最容易教人们使用的设计方式。

    • Syntax matters (often in perverse ways).

    • Preprocessor usage should be eliminated.
      预处理器的使用应该被消灭。

  • Low-level programming support rules:
    低层编程支持的规则:

    • Use traditional (dumb) linkers.

    • No gratuitous incompatibilities with C.
      不要无故引入与C语言的不兼容性。(注:然而 Bjarne 也提到,C语言在C99及之后的标准从C++借鉴了一些特性,但采用了不兼容的设计,这是C++无能为力的。)

    • Leave no room for a lower-level language below C++ (except assembler).
      不要给任何低层语言留出能存在于C++以下的的空间(除了汇编)。

    • What you don’t use, you don’t pay for (zero-overhead rule).
      你不会为不使用的(特性)付出代价(零额外开销原则)。(C++非常核心的一条原则)

    • If in doubt, provide means for manual control.
      如有不确定,提供手动控制的方法。

Bjarne 在后面又补充了两条:

  • There is a direct mapping of C++ language constructs to hardware.
    C++语言能直接映射到硬件。(核心竞争力之一)

  • The standard library is specified and implemented in C++.
    标准库使用C++自身声明并实现。(高效率,区别于一些高级语言)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值