Storage-Class Specifiers

The C++ storage-class specifiers tell the compiler about the duration and visibility of the object or function they declare, as well as where an object should be stored.

Syntax

storage-class-specifier:
auto
register
static
extern

Automatic Storage-Class Specifiers

The auto and register storage-class specifiers can be used only to declare names used in blocks or to declare formal arguments to functions. The term “auto” comes from the fact that storage for these objects is automatically allocated at run time (normally on the program's stack).

The auto Keyword

Few programmers use the auto keyword in declarations because all block-scoped objects not explicitly declared with another storage class are implicitly automatic. Therefore, the following two declarations are equivalent:

{

auto int i; // Explicitly declared as auto.

int j; // Implicitly auto.

}

The register Keyword

The register keyword is similar to the auto keyword except that it tells the compiler to keep the object in a machine register if one is available. If no register is available, the compiler treats the object as any other automatic object.

The benefits of the register storage class are increased speed and reduced demands on the application stack. The latter benefit is particularly useful in recursive algorithms.

Microsoft Specific

The global register-allocation optimization (/Oe option) instructs the compiler to ignore the registerkeyword and perform register allocation based on code analysis. For algorithms that depend on register allocation, either compile without this option or use the optimize pragma to shut off global register allocation.¨ANSI C does not allow for taking the address of a register object; this restriction does not apply to C++. However, if the address-of operator (&) is used on an object, the compiler must put the object in a location for which an address can be represented—in practice, this means in memory instead of in a register.

Static Storage-Class Specifiers

The static storage-class specifiers, static and extern, can be applied to objects and functions. Table 6.1 shows where the keywords static and extern can and cannot be used.

Table 6.1 Use of static and extern

Construct Can static Be Used? Can extern Be Used?

Function declarations within a block No No
Formal arguments to a function No No
Objects in a block Yes No
Objects outside a block Yes Yes
Functions Yes Yes
Class member functions Yes No
Class member data Yes No
typedef names No No

A name specified using the static keyword has internal linkage. That is, it is not visible outside the current translation unit. A name specified using the extern keyword has external linkage unless previously defined as having internal linkage. For more information about the visibility of names, see “Scope” and “Program and Linkage” in Chapter 2.

Note :

Functions that are declared as inline and that are not class member functions are given the same linkage characteristics as functions declared as static.

A class name whose declaration has not yet been encountered by the compiler can be used in an extern declaration. The name introduced with such a declaration cannot be used until the class declaration has been encountered.

Names Without Storage-Class Specifiers

File-scope names with no explicit storage-class specifiers have external linkage unless they are:

Declared using the const keyword

Previously declared with internal linkage