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++20) |
-
|
(since C++11) |
-
[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.
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.
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:
-