Dela via


Declarations

Declarations introduce new names into a program. Topics covered in this section include the following uses for declarations.

In addition to introducing a new name, a declaration specifies how an identifier is to be interpreted by the compiler. Declarations do not automatically reserve storage associated with the identifier. Definitions reserve storage.

Note

Most declarations are also definitions. Declarations that are not definitions include class declarations without the member list, and function declarations without the function body.

A declaration can be one of:

[ decl-specifiers ] [ declarator-list ] ;

function-definition

linkage-specification

template-specification

alias templates

explicit-template-instantiation

explicit-template-specialization

namespace-definition

namespace-alias-definition

alias-declaration

using-declaration

using-directive

asm-definition

The decl-specifiers component of a declaration is shown as optional; however, it can be omitted only in declarations of class types or enumerations.

The declarators in the declarator-list component contain the names being declared. Although the declarator-list is shown as optional, it can be omitted only in declarations or definitions of a function.

Note

The declaration of a function is often called a prototype. A prototype provides type information about arguments and the return type of the function. In addition, a prototype enables the compiler to perform correct conversions and helps provide type safety.

A name is considered to be declared immediately after its declarator but before its (optional) initializer. For more information, see Point of Declaration.

Declarations occur in a scope. The scope controls the visibility of the name declared and the duration of the object defined, if any. For more information about how scope rules interact with declarations, see Scope.

An object declaration is also a definition unless it contains the extern storage-class specifier described in Storage-Class Specifiers. A function declaration is also a definition unless it is a prototype. A prototype is a function header without a defining function body. The definition of an object causes allocation of storage and appropriate initializations for that object.

For information about function-definition, see C++ Function Definitions.

For information about linkage-specification, see Linkage Specifications.

For information about template-specification, explicit-template-instantiation and explicit-template-specialization, see Templates.

For information about namespace-definition and namespace-alias-definition, see Namespaces (C++).

For information about asm-definition, see __asm.

See Also

Other Resources

C++ Language Reference