C++ name convention
本节对C++ name convention进行描述。
Header file
C++的header file的name convention是不带.h
;C++是C的超集,所以它会包含C的header file,对于这种情况,name convention为c***
:
C | C++ |
---|---|
string.h |
cstring |
stdlib.h |
cstdlib |
_t
and _type
suffix
参考:
- stackoverflow C++ type suffix _t, _type or none
_t
suffix
stackoverflow C++ type suffix _t, _type or none:
in C++14 _t
suffix is commonly used to indicate an alias template for nested ::type
typedef, e.g. X_t<T>
stands for typename X<T>::type
– Piotr Skotnicki Oct 16 '14 at 9:22
A:
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).
_type
suffix
stackoverflow C++ type suffix _t, _type or none:
It seems _type
is more common in template meta-programming (for template class member typedefs), whereas no suffix is used when it is not important for the outside whether the typename is a class or a typedef. For (template) class member types there just seem to be some edge cases when it isn't clear which would be more appropriate. – tmlen Oct 16 '14 at 9:19
A:
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.
_t
and _v
suffix in trait
在 "tartanllama Detection Idiom - A Stopgap for Concepts # Metaprogramming demystified # Type traits and _v
and _t
suffixes" 段中,对此有着非常好的总结。
tartanllama Detection Idiom - A Stopgap for Concepts # Metaprogramming demystified # Type traits and _v
and _t
suffixes
A type trait is some template which can be used to get information about characteristics of a type. For example, you can find out if some type is an arithmetic type using std::is_arithmetic
:
template <class T>
void foo(T t) {
static_assert(std::is_arithmetic<T>::value, "Argument must be of an arithmetic type");
}
Type traits either “return” types with a ::type
member alias, or values with a ::value
alias. _t
and _v
suffixes are shorthand for these respectively. So std::is_arithmetic_v<T>
is the same as std::is_arithmetic<T>::value
, std::add_pointer_t<T>
is the same as typename std::add_pointer<T>::type
1.