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 fromias 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::stringis an alias forstd::basic_string<char>,std::string_viewis 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_typescoped_- 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*Exceptionin other languagesthere is no pattern for interface class names like
I*or*Interfacein 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_addressmake_*- 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_implappended (ordo_prepended); very common in boost libraries, also used in standard libarry*_castfor type convertions (based on 4 cast keywords:static_cast,dynamic_cast,const_castandreinterpret_cast), other examples:std::bit_cast,boost::lexical_cast,boost::numeric_cast
Templates
T,U,V, ... - similarly to loops, likely originated fromTas 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_caseinstead (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)