Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ type suffix _t, _type or none

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?

like image 778
tmlen Avatar asked Oct 16 '14 09:10

tmlen


People also ask

What does _t in C mean?

The _t usually wraps an opaque type definition. GCC merely add names that end with _t to the reserved namespace you may not use, to avoid conflicts with future versions of Standard C and POSIX (GNU C library manual).

What is _t in uint32_t?

The _t data types are typedef types in the stdint. h header, while int is a built-in fundamental data type to C. The _t datatypes are only available if stdint. h exists. The fundamental data types like int , however, are guaranteed to exist.


2 Answers

As @MarcoA.'s answer correctly points out, the suffix _t is largely inherited from C (and in the global namespace - reserved for POSIX).

This leaves us with "no suffix" and _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.

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.


* 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.

like image 78
Angew is no longer proud of SO Avatar answered Sep 20 '22 00:09

Angew is no longer proud of SO


As a C heritage the _t (that used to mean "defined via typedef") syntax has been inherited (they're also SUS/POSIX-reserved in the global namespace).

Types added in C++ and not present in the original C language (e.g. size_type) don't need to be shortened.

Keep in mind that to the best of my knowledge this is more of an observation on an established convention rather than a general rule.

like image 23
Marco A. Avatar answered Sep 21 '22 00:09

Marco A.