C ++类型后缀_t,_type或无 [英] C++ type suffix _t, _type or none

查看:175
本文介绍了C ++类型后缀_t,_type或无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ++类型后缀_t,_type或无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆