大头龙仔Blog

A strong man can save himself. A great man can save another.

关于更好的编程风格的建议 v1.0(zz)

| Comments

[C/C++] - Tips for Better Coding Style
关于更好的编程风格的建议 (v1.0)
Translated By Phoenix(phoenix8848@gmail.com)
In this entry, I show you 4 tips that address frequently asked questions from C++ programmers of all levels of expertise. It’s surprising to discover how many experienced programmers are still unaware of the deprecation of the .h notation of standard header files, the proper usage of namespaces, and the rules regarding binding of references to temporary objects, for example. These issues and others will be discussed here.
在这篇文章里我将谈谈各种层次的C++程序员经常问及的四个问题。例如我很惊讶地发现还有很多程序员没有意识到标准头文件扩展名.h的争议,命名空间的恰当用法以及引用临时对象的规则。这些问题及其它将在这里进行讨论。

First, we start by explaining the difference between the deprecated “xxx.h” header names and the modern, standard-compliant “xxx” header-naming notation. Next, we explore a few dark corners of C++ which due to compilers’ limitations and the somewhat recondite nature of the associated language rules tend(原文为“rulestend”) to confuse many programmers, e.g., the notion of comma-separated expressions and the rules of binding references to rvalues. Finally, we will learn how to invoke a function prior to a program’s startup.

首先我们从解释受非议的“XXX.h”头文件名与现代、符合标准的“<XXX>”头文件名记号之间的区别开始。接下来我们探索C++不为人知 的角落,由于编译器的局限性和关联语言规则某些隐蔽的自然特性迷惑了许多程序员,比如逗号分隔表达式的意义与引用型变量的规则。最后我们将学习如何在程序 启动前启动一个函数。

Tip 1: “iostream.h” or “iostream”?

语题1:“iostream.h” or “iostream”?

Many C++ programmers still use “iostream.h” instead of the newer, standard compliant “iostream” library. What are the differences between the two? First, the .h notation of standard header files was deprecated more than five years ago. Using deprecated features in new code is never a good idea. In terms of functionality, “iostream” contains a set of templatized I/O classes which support both narrow and wide characters, as opposed to “iostream.h” which only supports char-oriented streams. Third, the C++ standard specification of iostream’s interface was changed in many subtle aspects. Consequently, the interfaces and implementation of “iostream” differ from those of “iostream.h”. Finally, “iostream” components are declared in namespace std whereas “iostream.h” components are global.

很多C++程序员还在使用“iostream.h”代替新的符合标准的“iostream”库。两者有什么区别呢?首先,标准头文件“.h”扩展名在五年 前就倍受争议。在新代码中使用有争议的(过时的)特性永远都不是一个好主意。从本质上看,“iostream”包括一系列支持窄字符与宽字符的模板化 (templatized) I/O输入输出类,而相反地,“iostream.h”只支持字符流。第三,iostream接口的标准C++规范在许多细节方面进行了变动。因 此,“iostream”的接口与实现同那些“iostream.h”是有区别的。最后,“iostream”是在std命名空间中定义的而 “iostream.h”则是全局的。

Because of these substantial differences, you cannot mix the two libraries in one program. As a rule, use “iostream” unless you’re dealing with legacy code that is only compatible with “iostream.h”.

由于这些本质方面的不同,不能在同一程序中混合使用这两种库。结论为除非在处理与“iostream”保持兼容的历史遗留代码时否则最好使用“iostream.h”。

Tip 2: Binding a Reference to an R-Value

话题2:将引用与右值绑定

(R-Value:右值,与“左值”相对。例如x=3中,“x”是一个“左值”,“3”是一个右值。从本质上讲,左值是一个内存的地址,右值是一个实际的二进制值。)

R-Values and L-Values are a fundamental concept of C++ programming. In essence, an R-Value is an expression that cannot appear on the left-hand side of an assignment expression. By contrast, an L-Value refers to an object (in its wider sense), or a chunk of memory, to which you can write a value. References can be bound to both R-Values and L-Values. However, due to the language’s restrictions regarding R-Values, you have to be aware of the restrictions on binding references to R-Values, too.

右值和左值是C++编程的一个基本概念。本质上来讲右值是一个不可能出现在等号左边的表达式。相反,左值引用一个对象(广义范围上的),或者一块可读写的 内存。引用既可以指向右值也可以指向左值。然而,由于语言在处理右值上的限制,你也得在将引用指向右值是慎重考虑。

Binding a reference to an R-Value is allowed as long as the reference is bound to a const type. The rationale behind this rule is straightforward: you can’t change an R-Value, and only a reference to const ensures that the program doesn’t modify an R-Value through its reference. In the following example, the function f() takes a reference to const int:

将引用与右值绑定像引用静态类型一样也是被允许的。这条原则背后的原理是很显而易见的:你无法改变右值,因为对静态类型的引用确保程序不会通过这个接口改变右值。下面的例子,f()函数包含一个对静态整型变量的引用。