C ++有时在类型定义上使用后缀 _type (例如, std :: vector< T> :: value_type ),有时也会 _t (例如 std :: size_t ),或者没有后缀(普通类,还有typedef,如 std :: string ,它实际上是 std :: basic_string< ...> )
C++ sometimes uses the suffix _type on type definitions (e.g. std::vector<T>::value_type), also sometimes _t (e.g. std::size_t), or no suffix (normal classes, and also typedefs like std::string which is really std::basic_string<...>)
何时使用哪个名称是否有好的约定?
Are there any good conventions on when to use which name?
推荐答案正如@MarcoA。的答案正确指出的那样,后缀 _t 主要从C继承(在全局命名空间中-保留给POSIX)。
As @MarcoA.'s answer correctly points out, the suffix _t is largely inherited from C (and in the global namespace - reserved for POSIX).
这给我们留下了没有后缀和 _type 。
This leaves us with "no suffix" and _type.
注意 std 中没有以 _type * 结尾的名称空间范围名称;所有这些名称都是类和类模板的成员(或者,如果是与正则表达式相关的类型,则是嵌套的命名空间(在很大程度上起着类的作用))。我认为这是区别:类型本身不使用 _type 后缀。
Notice that there is no namespace-scope name in std ending in _type*; all such names are members of classes and class templates (or, in the case of regex-related types, of a nested namespace which largely plays a role of a class). I think that's the distinction: types themselves don't use the _type suffix.
后缀 _type 仅用于表示类型的成员,而且,通常在它们表示包含类的外部类型的成员时使用。比较 std :: vector< T> :: value_type 和 std :: vector< T> :: size_type ,其中分别来自向量的模板参数 T 和 Allocator ,与 std :: vector< T> :: iterator ,这是向量类模板的本征。
The suffix _type is only used on members which denote types, and moreover, usually when they denote a type somewhat "external" to the containing class. Compare std::vector<T>::value_type and std::vector<T>::size_type, which come from the vector's template parameters T and Allocator, respectively, against std::vector<T>::iterator, which is "intrinsic" to the vector class template.
*并非完全正确,有一些这样的名称(也在@jrok的注释中指出): common_type , underlying_type , is_literal_type , true_type , false_type 。在前三个中, _type 并不是真正的后缀,它是名称的实际组成部分(例如, metafunction 提供通用的类型或基础的类型)。使用 true_type 和 false_type ,它的确是后缀(因为 true 和 false 是保留字)。我会说这是一个 type ,它表示基于类型的元编程意义上的真/假值。
* Not entirely true, there are a few such names (also pointed out in a comment by @jrok): common_type, underlying_type, is_literal_type, true_type, false_type. In the first three, _type is not really a suffix, it's an actual part of the name (e.g. a metafunction to give the common type or the underlying type). With true_type and false_type, it is indeed a suffix (since true and false are reserved words). I would say it's a type which represents a true/false value in the type-based metaprogramming sense.
更多推荐
C ++类型后缀
发布评论