2.9. Details
2.9细节
We've covered a lot of ground in this chapter; the journey from traits to metafunctions takes us from the ad hoc type associations used in the simplest generic programs, to the fundamentals that allow metaprogramming to be viewed as a first-class coding activity. We also dipped into the mechanics of C++ templates, got an overview of the type traits library, and saw some of its components in action. In such a broad landscape, some important details are bound to be missed; we'll fill them in as we review the chapter's most important points.
我们在这章包含了许多的基本;从特性到元函数的过程使用我们从与装箱单的范型编程向着基本的元编程的转变.元编程被认为是第一类活跃代码.我们深入到C++模板的机制,对类型特性库做一个总结,并且看一些这样的元件的实现.在这样宽广的前景下,一些重要的细节将必定被忽略.当我们复习这一章的重点时候,我们将把这些细节拾起来.
偏特化
The meaning of specialization as applied to C++ templates can be tough to get a handle on, because it's used in two different ways. Its first usage refers to a template with specific arguments filled in, as in iterator_traits<int*>. In other words, a template specialization names the actual class (or function, in the case of function template specializations) that results from replacing its parameters with specific arguments.
特化的含义被应用到了C++模板当中.因为模板被用到两种不同的方法,所以C++模板可能是一项难以理解的技术.它的第一项作用是引用到一个具有特殊参数的模板,如iterator_trits<int*>.换句话说,模板特化使实际类具有将它的类型参数替换为指定参数类型类的名称.(或者在函数中,函数模板指定)
The second use of specialization shows up in "explicit specialization" and "partial specialization"; we showed both explicit and partial specializations of iterator_traits in this chapter. The name "explicit" is probably not well-chosen, since partial specializations are just as explicit; you can think of "explicit specialization" as meaning "full specialization" without any loss of understanding.
第二种使用特化的方法在“显式特化”和“偏特化”中显示。我们在这章中,显示了iterator_traits的显示和偏特化。由于偏特化仅仅作为显式,所以“显式”可能并没有好的被选择。你可以认为显式特化和全特化一样而没有任何的理解上面的损失。
To remember the syntax rules for declaring class template specializations (the second meaning), keep this form in mind:
为了回忆出申明的类的模板特化的语法规则(第二种意思),记住这个形式:
template <variable part>
struct template-name<fixed part>
In an explicit (or full) specialization, the variable part is empty and the fixed part consists of concrete template arguments. In a partial specialization, the variable part contains a parameter list, and at least one argument in the fixed part depends on those parameters.
在一个显式(全)特化当中,变量部分是空和包含具体模板参数的固定部分。在偏特化当中,变量部分包含一个参数列表,并且在指定这些参数的部分至少一个参数
主模板
The declaration of a template that is not a specialization (second meaning above) is called the primary template. We can think of the primary template as covering the general case, while specializations cover various special cases.
模板申明并不是一个特化(第二种意思),这个申明被称作为主模板。我们将这个主模板认作是一般形式,这个一般形式包含了各种特殊情况。
实例化
The moment the compiler needs to know much more about a template than what its arguments are—the names of its members or the identity of its base classes, for example—the template will be instantiated. At that point, the compiler fills in the actual values of all the template parameters, picks the best-matching explicit or partial specialization (if any), figures out all of the types and constants used in declarations in the template body, and rechecks those declarations for errors. It does not, however, instantiate definitions (such as member function bodies) until they are actually used. For example:
编译器需要知道关于模板的更多东西的时候,不仅知道它的参数——它的成员的名字还是它的基类的识别——模板将被实例化。就这而言,编译器用最匹配的显式的或者偏特化模板参数来填充实际类型(如果有的话),设想一下所有的类型和常数用在模板体的申明当中,并且重新核对这些申明来检查是否有错误。但是,编译器并不在它们使用之前实例化定义处的模板。例如:
struct X
... {
int f(T* x) // declaration
...{
U y[10]; // definition
return 0;
}
} ;
typedef X < int & , char > t1; // OK; no instantiation yet
t1 x1; // error: pointer to int& illegal
typedef X < int , char &> t2;
t2 x2; // OK; declarations check out
int a = x2.f(); // error: array of char& illegal
As you can see, template instantiation can affect not only compilation speed, but whether your program compiles at all!
就和你看到的一样,模板实例化能够不仅影响编译速度,而且一样影响着对你程序的编译。
A class with lots of members (including member functions) is known in the object-oriented programming literature as a "blob" [BMMM98]. The members of a class are all "coupled" to one another, because they must be declared together. To avoid coupling and increase modularity, avoid this anti-pattern. Instead, define separate traits as independent metafunctions.
一个有着很多成员的类(包括成员函数)被很多的OOP认为是“blob”。由于一个类的成员必须被申明在一起,所以一个一个相互耦合。为了避免耦合和增加模块性,避免了非模式。相反,定义分开特性为独立的元函数。
元数据
A "value" that can be manipulated by the C++ compile-time machinery can be thought of as metadata. In template metaprogramming, the two most common kinds of metadata are types and integer (including bool) constants. The compile-time part of C++ is often referred to as a "pure functional language" because metadata is immutable and metafunctions can't have any side effects.
一个可以被C++编译时间机制操作的值可以被认为是元数据。在模板元编程当中,两个最普通的元数据种类为类型和整型(包括bool)常数。由于元数据是不变的并且函数没有任何副作用,所以C++的编译时间部分经常被用来作为纯函数语言来引用。
多态
Literally, "having many forms." In computer languages polymorphism has come to mean the ability to manipulate different types through a common interface. Having a consistent interface is the best way to ensure that code is reusable and components interoperate naturally. Because C++ templates do not inherently treat the different kinds of metadata polymorphically, MPL follows the convention of using type wrappers around non-type metadata. In particular, numerical metadata is represented as a type with a nested numeric constant member called ::value.
字面上,“具有很多形式”。在电脑语言中多态逐渐表示为通过一个共用接口来操作不同类型的能力。具有相同接口是最好的办法来确保代码被重用并且元件操作自然。由于C++模板并不由始多态处理不同类型的元数据,MPL利用传统的使用封装无类型的元数据来操作。特别地,数字元数据被描述成为一个具有内嵌型别的类型。该类型包含::value的常量成员。
元函数
A "function" that operates on metadata and can be "invoked" at compile time. For the remainder of this book, a template or class will only be called a metafunction if it has no non-type parameters and returns a type called type. The arguments to the class template are the inputs to the compile time computation, and the ::type member is its result. Thus, expressions like:
一个操作元数据的“函数”可以在编译斯被调用。在余下的章节中,如果模板或者类没有无类型参量并且返回一个叫type的类型的话,则只称它为元函数。模板类的参数为编译计算的输入参数,并且::type成员是它的结果。这样,表示为:
are analogous to runtime computations like:
与执行期的函数相似如:
Numerical metafunction
数字的元函数
A metafunction that returns a wrapper type for a numerical value. As a convenience, many numerical metafunctions themselves provide a nested ::value member, so that you can write:
一个返回包装数据值的类型的元函数。操作方便的,许多数字的元函数本身提供了一个内嵌的::value成员变量,因此你可以这样写:
instead of the more general:
取代更普通的:
if you want to access the numerical result directly.
如果你想直接地访问数字结果。
空元函数
Any class with a publicly accessible ::type can be used as a metafunction that accepts no arguments. As a consequence of this definition, any metafunction specialization (first meaning above), such as
任何具有公开的::type可以被用来作为元函数。该元函数接受无参数。作为这个定义的影响,任何元函数特化(上面讲的),例如:
boost::remove_pointer<char*>, is a legal nullary metafunction.
特性
A technique for establishing associations between pieces of metadata via class template specializations. A key feature of the traits idiom is that it's non-intrusive: We can establish a new mapping without modifying the associated items themselves. MPL metafunctions can be viewed as a special case of traits, where there is only one result value for any input.
一个在元数据和模板类特化之间建立联系的技术。一个重要关于traits的特性理念是它是非插入的:我们可以建立一个新的映射而不用改变相关的部分自己。MPL元函数可以被认为是特殊的关于tratis的例子,在这个例子当中仅仅有一个结果值来作为任意输入。
类型特性
The Boost Type Traits library is a metafunction library. It contains a few metafunctions for low-level type manipulation. For example, the result of add_reference is always a reference type:
Boost的Type Traits库是一个元函数库。它包含一些元函数。这些元函数用来操作低水平的类型。例如,add_reference的结果就总是一个引用类型:
boost::add_reference < int &> ::type // int&
The Type Traits library is mainly comprised of Boolean-valued metafunctions that can be used to answer questions about the fundamental properties of any type. For example:
Type Traits库主要包含BOOL值的函数。该函数可以用来回答关于任意类型的基本属性。例如:
.
boost::is_reference < int &> ::value // true
翻译的不好请大家见谅
TAGS:C++,boost,stl,traits,library,type,metafunction,元函数,模板函数,模板类,模板元编程