cpp/language/declarations

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. are declarations that are sufficient to use the entity identified by the name.

A declaration is one of the following:


 * (including )
 * (including )


 * Empty declaration
 * A function declaration without a :


 * This declaration must declare a constructor, destructor, or user-defined type . It can only be used as part of a, , or explicit instantiation.


 * (a declaration that can appear inside a ), which, in turn, can be one of the following:




 * simple declaration

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

Specifiers
Declaration specifiers is a sequence of the following whitespace-separated specifiers, in any order:


 * the specifier. If present, the entire declaration is a  and each declarator introduces a new type name, not an object or a function.
 * function specifiers, only allowed in.


 * the specifier, allowed in class and function declarations.


 * . Only one storage class specifier is allowed.
 * Type specifiers, 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 . Only the following specifiers are part of , in any order:
 * simple type specifier
 * simple type specifier
 * simple type specifier


 * previously declared class name (optionally )
 * previously declared enum name (optionally )
 * previously declared (optionally )
 * template name with template arguments (optionally, optionally using )


 * the keyword, , or , followed by the identifier (optionally ), previously defined as the name of a class, struct, or union.
 * the keyword, , or , followed by template name with template arguments (optionally , optionally using ), previously defined as the name of a class template.
 * the keyword followed by the identifier (optionally ), previously declared as the name of an enumeration.
 * only one type specifier is allowed in a decl-specifier-seq, with the following exceptions:
 * can be combined with any type specifier except itself.
 * can be combined with any type specifier except itself.
 * or can be combined with, , , or.
 * or can be combined with.
 * can be combined with.
 * or can be combined with, , , or.
 * or can be combined with.
 * can be combined with.

may appear in, in which case they apply to the type determined by the preceding specifiers.

Repetitions of any specifier in a, such as , or are errors.

Declarators
is a comma-separated sequence of one or more, which have the following syntax:

Each in an init-declarator sequence  is processed as if it were a standalone declaration with the same specifiers:.

Each declarator introduces exactly one object, reference, function, or (for typedef declarations) type alias, whose type is provided by and optionally modified by operators such as  (reference to) or  (array of) or  (function returning) in the declarator. These operators can be applied recursively, as shown below.

A is one of the following:

@1@ The that is declared. @2@ A declarator that uses a  defines or redeclares a previously declared   or. @3@, only appears in. @4@ : the declaration declares  as a pointer to the type determined by. @5@ : the declaration declares  as a pointer to member of  of type determined by. is a @6@ : the declaration declares  as an lvalue reference to the type determined by. @7@ : the declaration declares  as an rvalue reference to the type determined by. @8@ . any valid declarator, but if it begins with *, &, or &&, it has to be surrounded by parentheses. @9@ . any valid declarator, but if it begins with *, &, or &&, it has to be surrounded by parentheses.

is a sequence of qualifiers, where either qualifier may appear at most once in the sequence.