Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language"

From cppreference.com
< cpp
(Classes: +since c++11)
m (Added (CTAD) acronym explicitly.)
 
(213 intermediate revisions by 47 users not shown)
Line 1: Line 1:
 
{{title|C++ language}}
 
{{title|C++ language}}
{{cpp/language/sidebar}}
+
{{cpp/language/navbar}}
This is a brief reference of available C++ constructs.
+
  
===General topics===
+
This is a reference of the core C++ language constructs.
  
====[[cpp/preprocessor | Preprocessor]]====
+
{|cellpadding="5"
====[[cpp/comments | Comments]]====
+
|-valign="top"
====[[cpp/keywords | Keywords]]====
+
|
====[[cpp/language/ascii | ASCII chart]]====
+
'''{{rl|basic concepts|Basic concepts}}'''
====[[cpp/language/escape | Escape sequences]]====
+
<div class="mainpagediv">
====[[cpp/language/history | History of C++]]====
+
{{rlp|comment|Comments}}<br>
 +
{{rl|ascii|ASCII chart}}<br>
 +
{{rl|punctuators|Punctuation}}<br>
 +
{{rl|identifiers|Names and identifiers}}<br>
 +
{{rl|type|Types}} –
 +
{{rl|types|Fundamental types}}<br>
 +
{{rl|object|Object}} –
 +
{{rl|scope|Scope}} –
 +
{{rl|lifetime|Lifetime}}<br>
 +
{{rl|definition|Definitions and ODR}}<br>
 +
{{rl|lookup|Name lookup}}<br>
 +
{{nbsp|4}}{{rl|qualified lookup|qualified}} – {{rl|unqualified lookup|unqualified}} ({{rl|adl|ADL}})<br>
 +
{{rl|as if|As-if rule}}<br>
 +
{{rl|ub|Undefined behavior (UB)}}<br>
 +
{{rl|memory model|Memory}} – {{rl|multithread|Multithread}} {{mark c++11}}<br>
 +
{{rl|charset|Character sets and encodings}}<br>
 +
{{rl|translation phases|Phases of translation}}<br>
 +
{{rl|main function|The {{tt|main}} function}}<br>
 +
{{rl|modules|Modules}} {{mark c++20}}<br>
 +
</div>
  
===Flow control===
+
'''{{rlp|keyword|Keywords}}'''
 +
<div class="mainpagediv">
 +
<!---->
 +
</div>
  
====Conditional execution statements====
+
'''{{rlp|preprocessor|Preprocessor}}'''
 +
<div class="mainpagediv">
 +
{{rlp|preprocessor/conditional|{{tt|#if}} - {{tt|#ifdef}} - {{tt|#ifndef}} - {{tt|#elif}}}}<br>
 +
{{rlp|preprocessor/conditional|{{tt|#elifdef}} - {{tt|#elifndef}}}} {{mark c++23}}<br>
 +
{{rlp|preprocessor/replace|{{tt|#define}} - {{tt|#}} - {{tt|##}}}}<br>
 +
{{rlpt|preprocessor/include|#include}} -
 +
{{rlpt|preprocessor/impl|#pragma}}<br>
 +
{{rlpt|preprocessor/line|#line}} -
 +
{{rlpt|preprocessor/error|#error}}<br>
 +
{{rlpt|preprocessor/error|#warning}} {{mark c++23}}<br>
 +
</div>
  
Different code paths are executed according to the value of given expression
+
'''{{rl|expressions|Expressions}}'''
 +
<div class="mainpagediv">
 +
{{rl|value category|Value categories}}<br>
 +
{{rl|eval order|Evaluation order}}<br>
 +
{{rl|constant expression|Constant expressions}}<br>
 +
{{ls|cpp/language/expressions#Operators}}<br>
 +
{{nbsp|4}}{{rl|operator assignment|assignment}} –
 +
{{rl|operator arithmetic|arithmetic}}<br>
 +
{{nbsp|4}}{{rl|operator incdec|increment and decrement}}<br>
 +
{{nbsp|4}}{{rl|operator logical|logical}} –
 +
{{rl|operator comparison|comparison}}<br>
 +
{{nbsp|4}}{{rl|operator member access|member access}}<br>
 +
{{nbsp|4}}{{rl|operator other|call, comma, ternary}}<br>
 +
{{nbsp|4}}{{rlt|sizeof}} – {{rlt|alignof}} {{mark c++11}}<br>
 +
{{nbsp|4}}{{rlt|new}} – {{rlt|delete}} – {{rlt|typeid}}<br>
 +
{{nbsp|4}}{{rl|operator_alternative|alternative representation}}<br>
 +
{{rl|default comparisons|Default comparisons}} {{mark c++20}}<br>
 +
{{rl|operator precedence|Operator precedence}}<br>
 +
{{ls|cpp/language/expressions#Conversions}}<br>
 +
{{nbsp|4}}{{rl|implicit conversion|implicit}} – {{rl|explicit cast|explicit}}<br>
 +
{{nbsp|4}}{{rl|cast operator|user-defined}}<br>
 +
{{nbsp|4}}{{rl|usual arithmetic conversions|Usual arithmetic conversions}}<br>
 +
{{nbsp|4}}{{rlt|static_cast}} – {{rlt|dynamic_cast}}<br>
 +
{{nbsp|4}}{{rlt|const_cast}}<br>
 +
{{nbsp|4}}{{rlt|reinterpret_cast}}<br>
 +
{{ls|cpp/language/expressions#Literals}} ({{rl|escape|Escape sequences}})<br>
 +
{{nbsp|4}}{{rl|bool literal|boolean}} –
 +
{{rl|integer literal|integer}} –
 +
{{rl|floating literal|floating}}<br>
 +
{{nbsp|4}}{{rl|character literal|character}} –
 +
{{rl|string literal|string}}<br>
 +
{{nbsp|4}}{{rlt|nullptr}} {{mark c++11}}<br>
 +
{{nbsp|4}}{{rl|user literal|user-defined (UDL)}} {{mark c++11}}<br>
 +
</div>
 +
|
 +
'''{{rl|declarations|Declarations}}'''
 +
<div class="mainpagediv">
 +
{{rl|conflicting declarations|Conflicting declarations}}<br>
 +
{{rl|storage duration|Storage duration and linkage}}<br>
 +
{{rl|tu local|Translation-unit-local}} {{mark c++20}}<br>
 +
{{rl|language linkage|Language linkage}}<br>
 +
{{rl|namespace|Namespace declaration}}<br>
 +
{{rl|namespace alias|Namespace alias}}<br>
 +
{{rl|reference|References}} –
 +
{{rl|pointer|Pointers}} –
 +
{{rl|array|Arrays}}<br>
 +
{{rl|structured binding|Structured bindings}} {{mark c++17}}<br>
 +
{{rl|enum|Enumerations and enumerators}}<br>
 +
{{rl|inline|{{tt|inline}} specifier}}<br>
 +
{{rl|asm|Inline assembly}}<br>
 +
{{rl|cv|{{tt|const}}/{{tt|volatile}}}}<br>
 +
{{rlt|constexpr}} {{mark c++11}}<br>
 +
{{rlt|consteval}} {{mark c++20}}<br>
 +
{{rlt|constinit}} {{mark c++20}}<br>
 +
{{rlt|decltype}} {{mark c++11}}<br>
 +
{{rlt|auto}} {{mark c++11}}<br>
 +
{{rlt|typedef}} – {{rl|type alias|Type alias}} {{mark c++11}}<br>
 +
{{rl|elaborated type specifier|Elaborated type specifiers}}<br>
 +
{{rl|attributes|Attributes}} {{mark c++11}}<br>
 +
{{rlt|alignas}} {{mark c++11}}<br>
 +
{{rlt|static_assert}} {{mark c++11}}<br>
 +
</div>
  
* {{rl|if}} executes code conditionally
+
'''{{rl|initialization|Initialization}}'''
* {{rl|switch}} executes code according to the value of an integral argument
+
<div class="mainpagediv">
 +
{{rl|default initialization|Default-initialization}}<br>
 +
{{rl|value initialization|Value-initialization}}<br>
 +
{{rl|copy initialization|Copy-initialization}}<br>
 +
{{rl|direct initialization|Direct-initialization}}<br>
 +
{{rl|aggregate initialization|Aggregate initialization}}<br>
 +
{{rl|list initialization|List-initialization}} {{mark c++11}}<br>
 +
{{rl|reference initialization|Reference initialization}}<br>
 +
{{rl|initialization#Non-local variables|Static non-local initialization}}<br>
 +
{{nbsp|4}}{{rl|zero initialization|zero}} – {{rl|constant initialization|constant}}<br>
 +
{{rl|initialization#Non-local variables|Dynamic non-local initialization}}<br>
 +
{{nbsp|4}}{{rl|initialization#Non-local variables|ordered}} – {{rl|initialization#Non-local variables|unordered}}<br>
 +
{{rl|copy elision|Copy elision (RVO)}}
 +
</div>
  
====Iteration statements====
+
'''{{rl|functions|Functions}}'''
 +
<div class="mainpagediv">
 +
{{rl|function|Function declaration}}<br>
 +
{{rl|default arguments|Default arguments}}<br>
 +
{{rl|variadic arguments|Variadic arguments}}<br>
 +
{{rl|lambda|Lambda expression}} {{mark c++11}}<br>
 +
{{rl|overload resolution|Overload resolution}}<br>
 +
{{rl|operators|Operator overloading}}<br>
 +
{{rl|overloaded address|Address of an overload set}}<br>
 +
{{rl|coroutines|Coroutines}} {{mark c++20}}<br>
 +
</div>
  
The same code is executed several times
+
'''{{rl|statements|Statements}}'''
 +
<div class="mainpagediv">
 +
{{rlt|if}} –
 +
{{rlt|switch}}<br>
 +
{{rlt|for}} –
 +
{{rl|range-for|range-{{tt|for}}}} {{mark c++11}}<br>
 +
{{rlt|while}} – {{rl|do|{{tt|do}}-{{tt|while}}}}<br>
 +
{{rlt|continue}} –
 +
{{rlt|break}}<br>
 +
{{rlt|goto}} –
 +
{{rlt|return}}<br>
 +
{{rl|transactional memory|{{tt|synchronized}}/{{tt|atomic}}}} {{mark since tm ts}}<br>
 +
</div>
 +
|
 +
'''{{rl|classes|Classes}}'''
 +
<div class="mainpagediv">
 +
{{rl|class|Class types}} –
 +
{{rl|union|Union types}}<br>
 +
{{rl|injected-class-name}}<br>
 +
{{rl|data members|Data members}} –
 +
{{rl|bit field|Bit-fields}}<br>
 +
{{rl|member functions|Member functions}} –
 +
{{rl|this|The {{tt|this}} pointer}}<br>
 +
{{rl|static|Static members}} –
 +
{{rl|nested types|Nested classes}}<br>
 +
{{rl|derived class|Derived class}} –
 +
{{rl|using declaration|{{tt|using}}-declaration}}<br>
 +
{{rl|ebo|Empty base optimization (EBO)}}<br>
 +
{{rl|virtual|Virtual function}} –
 +
{{rl|abstract class|Abstract class (ABC)}}<br>
 +
{{rlt|override}} {{mark c++11}} – {{rlt|final}} {{mark c++11}}<br>
 +
{{rl|access|Member access}} –
 +
{{rlt|friend}}<br>
 +
{{rl|constructor|Constructors and member initializer lists}}<br>
 +
{{rl|default constructor|Default constructor}} –
 +
{{rl|destructor|Destructor}}<br>
 +
{{rl|copy constructor|Copy constructor}} –
 +
{{rl|copy assignment|Copy assignment}}<br>
 +
{{rl|move constructor|Move constructor}} {{mark c++11}}<br>
 +
{{rl|move assignment|Move assignment}} {{mark c++11}}<br>
 +
{{rl|converting constructor|Converting constructor}}<br>
 +
{{rl|explicit|{{tt|explicit}} specifier}}<br>
 +
</div>
  
* {{rl|for}} executes loop
+
'''{{rl|templates|Templates}}'''
* {{rl|range-for}} executes loop over a range {{mark since c++11}}
+
<div class="mainpagediv">
* {{rl|while}} executes loop, checking condition before each iteration
+
{{rl|template parameters|Template parameters and arguments}}<br>
* {{rl|do|do-while}} executes loop, checking condition after each iteration
+
{{rl|class template|Class template}} –
 +
{{rl|function template|Function template}}<br>
 +
{{rl|variable template|Variable template}} {{mark c++14}}<br>
 +
{{rl|member template|Class member template}}<br>
 +
'''Template argument deduction'''<br>
 +
{{nbsp|4}}{{rl|template argument deduction|function}} –
 +
{{rl|class template argument deduction|class (CTAD)}} {{mark c++17}}<br>
 +
{{rl|template specialization|Explicit specialization}} – {{rl|partial specialization|Partial specialization}}<br>
 +
{{rl|parameter pack|Parameter packs}} {{mark c++11}}<br>
 +
{{rlt|sizeof...}} {{mark c++11}}<br>
 +
{{rl|fold|Fold expressions}} {{mark c++17}}<br>
 +
{{rl|pack_indexing|Pack indexing}} {{mark c++26}}<br>
 +
{{rl|dependent name|Dependent names}} –
 +
{{rl|sfinae|SFINAE}}<br>
 +
{{rl|constraints|Constraints and concepts}} {{mark c++20}}<br>
 +
{{rl|requires|Requires expression}} {{mark c++20}}<br>
 +
</div>
  
====Jump statements====
+
'''{{rl|exceptions|Exceptions}}'''
 +
<div class="mainpagediv">
 +
{{rl|try|{{tt|try}} block}}<br>
 +
{{rl|throw|Throwing exceptions}}<br>
 +
{{rl|catch|Handling exceptions}}<br>
 +
'''Exception specification'''<br>
 +
{{nbsp|4}}{{rl|noexcept spec|{{tt|noexcept}} specification}} {{mark c++11}}<br>
 +
{{nbsp|4}}{{rl|except spec|Dynamic specification}} {{mark tooltip|until C++17|deprecated in C++11}}<br>
 +
{{rl|noexcept|{{tt|noexcept}} operator}} {{mark c++11}}<br>
 +
</div>
  
Continue execution at a different location
+
'''Miscellaneous'''
 +
<div class="mainpagediv">
 +
{{rl|history|History of C++}}<br>
 +
{{rl|extending std|Extending the namespace std}}<br>
 +
{{rl|acronyms|Acronyms}}:
 +
[[cpp/ranges/cpo|CPO]],
 +
[[cpp/language/ndr|IFNDR]],
 +
[[cpp/language/ndr|NDR]],
 +
[[cpp/string/byte|NTBS]],<br>
 +
[[cpp/language/template parameters#Non-type template parameter|NTTP]],
 +
[[cpp/ranges#Range adaptor objects|RAO]],
 +
[[cpp/memory/allocator_traits/select_on_container_copy_construction|SOCCC]],
 +
[[cpp/language/template metaprogramming|TMP]],
 +
[[cpp/language/translation phases#Phase 8|TU]]
 +
</div>
  
* {{rl|continue}} skips the remaining part of the enclosing loop body
+
'''Idioms'''
* {{rl|break}} terminates the enclosing loop
+
<div class="mainpagediv">
* {{rl|goto}} continues execution in another location
+
{{rl|crtp|Curiously Recurring Template Pattern (CRTP)}}<br>
* {{rl|return}} terminates execution of the enclosing function
+
{{rl|pimpl|Pointer to implementation (PIMPL)}}<br>
 +
{{rl|raii|Resource acquisition is initialization (RAII)}}<br>
 +
{{rl|rule of three|Rule of three/five/zero}}<br>
 +
{{rl|Zero-overhead principle|Zero-overhead principle}}<br>
 +
</div>
 +
|}
  
===Functions===
+
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/language|C language constructs|nomono=true}}
 +
{{dsc end}}
  
The same code can be reused at different locations in the program
+
{{langlinks|ar|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}}
 
+
* {{rl|function | function declaration}} declares a function
+
* {{rl|lambda | lambda function declaration}} declares a lambda function
+
* {{rl|function_template | function template}} declares a function template
+
* {{rl|inline | inline specifier}} hints the compiler to insert a function's body directly into the calling code
+
* {{rl|except_spec | exception specifications}} enforces the function to throw only specific exceptions or not to throw at all {{mark deprecated}}
+
* {{rl|noexcept_spec | noexcept specifier}} enforces the function not to throw any exceptions {{mark since c++11}}
+
 
+
===Exceptions===
+
 
+
Exceptions are a more robust way to signal error condition than function return codes or global error variables
+
 
+
* {{rl|throw | throw expression}} signals an error and transfers control to error handler
+
* {{rl|try_catch | try-catch block}} catches exceptions originating from specific block of code
+
 
+
===Namespaces===
+
 
+
Namespaces provide a way to prevent name clashes in large projects
+
 
+
* {{rl|namespace | namespace declaration}}
+
* {{rl|namespace_alias | namespace aliases}}
+
 
+
===Types===
+
 
+
* {{rl|types | fundamental types}} defines basic character, integer and floating point types
+
* {{rl|class | compound types}} defines types, holding several data members (essentially the same as ''class'')
+
* {{rl|enum | enumeration types}} defines types, that are able to hold only one of the specified values
+
* {{rl|union | union types}} defines types, that can hold data in several representations
+
* {{rl|function | function types}} define function call signatures, that is the types of arguments and the return type
+
* {{rl|decltype | decltype specifier}} defines a type, equivalent to the type of an expression {{mark since c++11}}
+
 
+
====Specifiers====
+
 
+
* {{rl|cv | cv specifiers}} specifies constness and volatility of a type
+
* {{rl|storage_duration | storage duration specifiers}} specifies storage duration of a type
+
* {{rl|constexpr | constexpr specifier}} specifies that the value of a variable or function can be computed at compile time {{mark since c++11}}
+
* {{rl|auto | auto specifier}} specifies that the actual type shall be defined from the expression, assigned to the variable {{mark since c++11}}
+
* {{rl|alignas | alignas specifier}} specifies that the storage for the variable should be aligned by specific amount {{mark since c++11}}
+
 
+
====Literals====
+
Literals are the tokens of a C++ program that represent constant values, embedded in the source code.
+
 
+
* {{rl|integer_literal|integer literals}} are decimal, octal, or hexadecimal numbers of integer type.
+
* {{rl|character_literal|character literals}} are individual characters of type {{cpp|char}}, {{cpp|char16_t}}, {{cpp|char32_t}}, or {{cpp|wchar_t}}.
+
* {{rl|floating_literal|floating-point literals}} are values of type {{cpp|float}}, {{cpp|double}}, or {{cpp|long double}}
+
* {{rl|string_literal|string literals}} are sequences of characters, which may be narrow, multibyte, or wide.
+
* {{rl|bool_literal|boolean literals}} are values of type {{cpp|bool}}, that is {{cpp|true}} and {{cpp|false}}
+
* {{rl|nullptr}} is the pointer literal which specifies a null pointer value {{mark since c++11}}
+
* {{rl|user_literal|user-defined literals}} are constant values of user-specified type {{mark since c++11}}
+
 
+
====Expressions====
+
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.
+
 
+
* {{rl|value_category|value categories}} (lvalue, rvalue, glvalue, prvalue, xvalue) classify expressions by their values.
+
* {{rl|eval_order|order of evaluation}} of arguments and subexpressions specified the order in which intermediate results are obtained.
+
* {{rl|operators}} allow the use of syntax commonly found in mathematics
+
{{cpp/language/operators}}
+
* {{rl|operator_precedence | operator precedence}} the order in which operators are bound to their arguments
+
* {{rl|operator_alternative | alternative representations}} alternative spellings for some of the operators
+
 
+
====Utilities====
+
 
+
; Types
+
* {{rl|typedef | typedef declaration}} creates a synonym for a type
+
* {{rl|type_alias | type alias declaration}} creates a synonym for a type
+
* {{rl|attributes | attributes}} defines additional information about variable {{mark since c++11}}
+
; Casts
+
* {{rl|implicit_cast | standard conversions}} implicit conversions from one type to another
+
* {{rl|const_cast | {{tt|const_cast}} conversion}}
+
* {{rl|static_cast | {{tt|static_cast}} conversion}}
+
* {{rl|dynamic_cast | {{tt|dynamic_cast}} conversion}}
+
* {{rl|reinterpret_cast | {{tt|reinterpret_cast}} conversion}}
+
; Memory allocation
+
* {{rl|new | new expression}} allocates memory dynamically
+
* {{rl|delete | delete expression}} deallocates memory dynamically
+
 
+
===Classes===
+
 
+
Classes provide the concept of object-oriented programming in C++
+
 
+
* {{rl|class | class declaration}}
+
* {{rl|this | {{tt|this}} pointer}} links to the current instance of a class in member methods
+
* {{rl|access | access specifiers}}
+
* {{rl|friend | friend specifier}} grants access privileges to private/protected parts for non-member classes or functions
+
* {{rl|initializer_list | initializer lists}}
+
* {{rl|virtual | virtual function specifier}} declares a virtual function
+
* {{rl|explicit | explicit function specifier}} declares that a constructor or conversion operator can not be used in implicit conversions {{mark since c++11}}
+
 
+
====Special member functions====
+
 
+
* {{rl|default_constructor | default constructor}} initializes the object with default contents
+
* {{rl|copy_constructor | copy constructor}} initializes the object with the contents of another object
+
* {{rl|move_constructor | move constructor}} initializes the object with the contents of other, temporary object, minimizing copying overhead {{mark since c++11}}
+
* {{rl|as_operator | assignment operator}} replaces the contents of the object with the contents of another object
+
* {{rl|move_operator | move assignment operator}} replaces the contents of the object with the contents of other, temporary object, minimizing copying overhead {{mark since c++11}}
+
* {{rl|destructor | destructor}} releases claimed resources
+
 
+
===Templates===
+
 
+
Allows functions and classes to operate on generic types
+
 
+
* {{rl|class_template | class template declaration}}
+
* {{rl|function_template | function template declaration}}
+
* {{rl|parameter_pack | parameter packs}} {{mark since c++11}}
+
 
+
===Miscellaneous===
+
 
+
* {{rl|asm|Inline assembly}}
+

Latest revision as of 11:11, 21 September 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 

This is a reference of the core C++ language constructs.

Basic concepts

Keywords

Preprocessor

Expressions

Declarations

Initialization

Functions

Statements

Classes

Templates

Exceptions

try block
Throwing exceptions
Handling exceptions
Exception specification
    noexcept specification (C++11)
    Dynamic specification (until C++17*)
noexcept operator (C++11)

Miscellaneous

Idioms

[edit] See also

C documentation for C language constructs