Udostępnij za pośrednictwem


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 — reserving storage is done by definitions.

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 may be one of:

[ decl-specifiers ] [ declarator-list ] ;
function-definition 
linkage-specification 
template-specification 
explicit-template-instantiation 
explicit-template-specialization 
namespace-definition 
namespace-alias-definition 
using-declaration 
using-directive 
asm-definition

The declarators in declarator-list 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." This declaration provides type information about arguments and the function's return type that allows the compiler to perform correct conversions and to help provide type safety.

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

Declarations occur in a scope. This 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 function header with no defining function body. An object's definition causes allocation of storage and appropriate initializations for that object.

For information on function definitions, see Function definitions.

For information on declaring linkage specifications, see linkage specifications.

For information on template specifications, explicit template instantiations and explicit template specializations, see Templates.

For information on namespace definitions and namespace alias definitions, see namespaces.

For information on asm definitions, see __asm.

See Also

Other Resources

C++ Language Reference