Namespaces
Variants
Views
Actions

Declarations

From cppreference.com
< cpp‎ | language
 
 
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
 
 

Declarations are how names are introduced (or re-introduced) into the C++ program. Not all declarations actually declare anything, and each kind of entity is declared differently. Definitions are declarations that are sufficient to use the entity identified by the name.

A declaration is one of the following:

  • Attribute declaration (attr ;)
(since C++11)
  • Empty declaration (;)
  • A function declaration without a decl-specifier-seq :
attr (optional) declarator ;
attr - (since C++11) sequence of any number of attributes
declarator - a function declarator
This declaration must declare a constructor, destructor, or user-defined type conversion function. It can only be used as part of a template declaration, explicit specialization, or explicit instantiation.
  • block-declaration (a declaration that can appear inside a block), which, in turn, can be one of the following:
(since C++11)
(since C++20)
(since C++11)
  • simple declaration

Contents

[edit] Simple declaration

A simple declaration is a statement that introduces, creates, and optionally initializes one or several identifiers, typically variables.

decl-specifier-seq init-declarator-list (optional) ; (1)
attr decl-specifier-seq init-declarator-list ; (2) (since C++11)
decl-specifier-seq - sequence of specifiers
init-declarator-list - comma-separated list of init-declarator s (see below)
attr - sequence of any number of attributes


init-declarator-list can only be omitted when declaring a named class or a named enumeration.

A structured binding declaration is also a simple declaration.

(since C++17)


The syntax of init-declarator is defined as follows:

declarator initializer (1)
declarator requires-clause (optional) contract-specs (optional) (2)
1) A declarator with an initializer.
2) A declarator without an initializer.
declarator - a declarator
initializer - an initializer
requires-clause - (since C++20) a requires clause
contract-specs - (since C++26) a list of function contract specifiers


requires-clause can only appear if declarator declares a templated function.

(since C++20)

contract-specs can only appear if declarator declares a function or function template.

(since C++26)

[edit] Specifiers

Declaration specifiers (decl-specifier-seq) is a sequence of the following whitespace-separated specifiers, in any order:

  • the inline specifier is also allowed on variable declarations.
(since C++17)
  • the friend specifier, allowed in class and function declarations.
  • the constexpr specifier, only allowed in variable definitions, function and function template declarations, and the declaration of static data members of literal type.
(since C++11)
  • the consteval specifier, only allowed in function and function template declarations.
  • the constinit specifier, only allowed in declaration of a variable with static or thread storage duration. At most one of the constexpr, consteval, and constinit specifiers is allowed to appear in a decl-specifier-seq.
(since C++20)
  • storage class specifier (register, (until C++17) static, thread_local, (since C++11) extern, mutable). Only one storage class specifier is allowed, except that thread_local may appear together with extern or static(since C++11).
  • Type specifiers (type-specifier-seq), a sequence of specifiers that names a type. The type of every entity introduced by the declaration is this type, optionally modified by the declarator (see below). This sequence of specifiers is also used by type-id. Only the following specifiers are part of type-specifier-seq, in any order: