This is a brief reference of available C++ language constructs.
Conditional execution statements
Conditional statements execute different code paths according to the value of given expression.
Iteration statements execute a code path multiple times.
- for executes loops by specifying initialization, comparison, and increment
- range-for executes loops over a range (since C++11)
- while executes loop, checking condition before each iteration
- do-while executes loop, checking condition after each iteration
Jump statements continue program execution at a different location.
- continue skips the remaining part of the enclosing loop body
- break terminates the enclosing loop
- goto continues execution in another location
- return terminates execution of the enclosing function
The same code can be reused at different locations in the program.
- function declarations declare functions
- lambda function declarations declare lambda functions (since C++11)
- function templates declare function templates
- inline specifier hints that the compiler insert a function's body directly into the calling code
- exception specifications declare that a function throws only specific exceptions (deprecated)
- noexcept specifier declares whether or not a function throws any exceptions (since C++11)
Exceptions are a more robust way to signal error condition than function return codes or global error variables.
- throw expressions signal errors and transfer control to error handlers
- try-catch blocks catch exceptions originating from specific blocks of code
- noexcept specifier and noexcept operator define and test if expressions throw exceptions (since C++11)
Namespaces provide a way to prevent name clashes in large projects.
- namespace declarations declare namespaces
- namespace aliases declare alternate names for existing namespaces
- fundamental types define basic character, integer and floating point types
- pointer types define types holding a memory location
- compound types define types that hold several data members (essentially the same as class)
- enumeration types define types that are able to hold only one of the specified values
- union types define types that can hold data in several representations
- function types define function call signatures, that is the types of arguments and the return type
- decltype specifier defines a type equivalent to the type of an expression (since C++11)
- cv specifiers specify constness and volatility of types
- storage duration specifiers specify storage duration of types
- constexpr specifier specifies that the value of a variable or function can be computed at compile time (since C++11)
- auto specifier specifies that the actual type shall be defined from the expression, assigned to the variable (since C++11)
- alignas specifier specifies that the storage for the variable should be aligned by specific amount (since C++11)
Whenever a named variable is declared, and whenever a temporary object is created, the initial value of the new object is provided through one of the following mechanisms:
- default initialization occurs when no initializer is provided
- value initialization occurs when the initializer is an empty set of parentheses
- zero initialization initializes every bit of the object to zero
- copy initialization initializes an object from another object
- direct initialization provides the initial value or the list of constructor arguments, in parentheses
- aggregate initialization provides initial values to every member of an array or a struct without a constructor
- list initialization provides an arbitrary long list of values, which can initialize a std::vector or std::map (since C++11)
- constant initialization initializes all constant static objects before anything else
- reference initialization binds references to objects and extend the lifetimes of the temporaries
Literals are the tokens of a C++ program that represent constant values, embedded in the source code.
- integer literals are decimal, octal, or hexadecimal numbers of integer type.
- character literals are individual characters of type char, char16_t, char32_t, or wchar_t
- floating-point literals are values of type float, double, or long double
- string literals are sequences of characters, which may be narrow, multibyte, or wide
- boolean literals are values of type bool, that is true and false
- nullptr is the pointer literal which specifies a null pointer value (since C++11)
- user-defined literals are constant values of user-specified type (since C++11)
An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.
- value categories (lvalue, rvalue, glvalue, prvalue, xvalue) classify expressions by their values
- order of evaluation of arguments and subexpressions specify the order in which intermediate results are obtained
- operators allow the use of syntax commonly found in mathematics
a = b
a == b
static_cast converts one type to another compatible type
- operator precedence defines the order in which operators are bound to their arguments
- alternative representations are alternative spellings for some operators
- typedef declarations create synonyms for types
- type alias declarations create synonyms for types
- attributes define additional information about variables (since C++11)
- standard conversions implicit conversions from one type to another
- explicit cast conversion using C-style cast notation and functional notation
- Memory allocation
Classes provide the concept of object-oriented programming in C++.
- class declarations declare classes
thispointer links to the current instance of a class in member methods
- access specifiers determine visibility of class members
- friend specifier grants access privileges to private/protected parts for non-member classes or functions
- initializer lists initialize class member data
Class-specific function properties
- virtual function specifier declares that a function is virtual
- override specifier declares that a virtual function overrides another virtual function.(since C++11)
- final specifier declares that a virtual function can not be overridden in a inheriting class.(since C++11)
- explicit function specifier declares that a constructor or conversion operator can not be used in implicit conversions (since C++11)
- static function specifier declares that a function does not use class data
- cv function specifier declares that a member function can only be used on cv qualified objects
Special member functions
- default constructor initializes the object with default contents
- copy constructor initializes the object with the contents of another object
- move constructor initializes the object with the contents of other, temporary object, minimizing copying overhead (since C++11)
- copy assignment operator replaces the contents of the object with the contents of another object
- move assignment operator replaces the contents of the object with the contents of other, temporary object, minimizing copying overhead (since C++11)
- destructor releases claimed resources
Allows functions and classes to operate on generic types
- class template declaration declares a class template
- function template declaration declares a function template
- template specialization defines an existing template for a specific type
- dependent names change the behavior of the template when instantiated
- parameter packs allows the use of lists of types in templates (since C++11)
Start and termination
- a main function provides a designated starting point for the program
- non-local variable initialization describes how static and thread-local variables (since C++11) are initialized
- termination describes the process of program shutdown
- the as-if rule allows any code transformation that doesn't change the output
- copy elision, including RVO and NRVO, makes pass-by-value the preferred approach in many situations.
- empty base optimization makes multiple inheritance from interfaces or policy classes overhead-free and is required for standard-layout types.
- Inline assembly allows the use of assembly code alongside C++ code