naming
C++ has a lot of naming conventions, partially inherited from C. This page aims to list them all to help beginners and people coming from other languages better name stuff in C++ code.
Some of these names are used pretty much always, some only when then there is no better alternative.
Main function
argc
- argument count variable nameargv
- argument values variable name
Examples:
int main(int argc, char** argv)
int main(int argc, char* argv[])
Variable names
When we want to concisely express variable purpose.
str
- stringsv
- string viewptr
,p
- pointern
,len
,sz
- length/size (usually some integer type)bufsz
- size of a bufferi
,j
,k
, ... - loop control variables - probably originated fromi
as a shortcut for iteration or index - this convention applies to many programming languagesx
,y
,z
,w
,u
,v
- anything related to position and other things closely related to math, sometimes also loop counters if iterating over specific axisfirst
,last
,it
- counters in iterator-based loops, variables/parameters in algorithmslhs
,rhs
(left/right hand side) - used for operator overloading and comparisons - use these if you have no better names to represent 2 different objects of the same type
Type names
basic_*
- for any class template that is expected to be instantiated with different template parameters, e.g.std::string
is an alias forstd::basic_string<char>
,std::string_view
is an alias forstd::basic_string_view<char>
*_type
- member type aliases, e.g. if you have a class that holds some objects in a very specific container (which may have very long name) you should define a member type alias (inside the class) namedcontainer_type
scoped_
- RAII wrappers around specific tasks (e.g.std::scoped_lock
,std::scoped_allocator_adaptor
)there is no pattern for exception class names like
E*
or*Exception
in other languagesthere is no pattern for interface class names like
I*
or*Interface
in other languages
Function names
to_*
- common name for functions which transform a single object to a different type, e.g.std::to_string
,std::to_chars
,std::to_address
make_*
- common name for factory functions which create an object of specific type (often from multiple arguments) (often multiple overloads), e.g.std::make_unique
,std::make_shared
,std::make_pair
,std::make_tuple
,std::make_optional
,std::make_error_code
*_impl
- common where a function (or class) implementation needs to be split, the implementation code has the same name with_impl
appended (ordo_
prepended); very common in boost libraries, also used in standard libarry*_cast
for type convertions (based on 4 cast keywords:static_cast
,dynamic_cast
,const_cast
andreinterpret_cast
), other examples:std::bit_cast
,boost::lexical_cast
,boost::numeric_cast
Templates
T
,U
,V
, ... - similarly to loops, likely originated fromT
as a shortcut for type or template, used as template type parameter namesForwardIterator
,UnaryPredicate
,TriviallyCopyable
- concept names for types that are expected to satisfy certain requirements; since C++20 no longer a convention but actual language feature; while aliases in templates traditionally usedPascalCase
, concept names in the standard library usesnake_case
instead (any custom concept definitions should too)Ts
,Args
, any noun in plural form - to name parameter pack in variadic templates*_t
- types found inside type traits*_v
- values found inside type traits
Other
impl
,detail
- common names for namespaces that indicate implementation details (code inside is not a part of public API and has no documentation)