Certain C++ keywords, called storage class specifiers and cv-qualifiers, provide additional information about storage. Here’s a list of the storage class specifiers:
auto (eliminated as a specifier in C++11)
register
static
extern
thread_local (added by C++11)
mutable
You’ve already seen most of these, and you can use no more than one of them in a single declaration, except that thread_local
can be used with static
or extern
. To review, prior to C++11, the keyword auto
could be used in a declaration to document that the variable is an automatic variable. (In C++11, auto
is used for automatic type deduction.) The keyword register
is used in a declaration to indicate the register storage class, which, in C++11, simply is an explicit way of saying the variable is automatic. The keyword static
, when used with a file-scope declaration, indicates internal linkage. When used with a local declaration, it indicates static storage duration for a local variable. The keyword extern
indicates a reference declaration—that is, that the declaration refers to a variable defined elsewhere. The keyword thread_local
indicates that the duration of the variable is the duration of the containing thread. A thread_local
variable is to a thread much as a regular static variable is to the whole program. The keyword mutable
is explained in terms of const
, so let’s look at the cv-qualifiers first before returning to mutable
.
Here are the cv-qualifiers:
const
volatile
(As you may have guessed, cv stands for const
and volatile
.) The most commonly used cv-qualifier is const
, and you’ve already seen its purpose: It indicates that memory, after initialized, should not be altered by a program. We’ll come back to const
in a moment.
The volatile
keyword indicates that the value in a memory location can be altered even though nothing in the program code modifies the contents. This is less mysterious than it sounds. For example, you could have a pointer to a hardware location that contains the time or information from a port. In this case, the hardware, not the program, changes the contents. Or two programs may interact, sharing data. The intent of this keyword is to improve the optimization abilities of compilers. For example, suppose the compiler notices that a program uses the value of a particular variable twice within a few statements. Rather than have the program look up the value twice, the compiler might cache the value in a register. This optimization assumes that the value of the variable doesn’t change between the two uses. If you don’t declare a variable as volatile
, then the compiler can feel free to make this optimization. If you do declare a variable as volatile
, you’re telling the compiler not to make that sort of optimization.
mutable
Now let’s return to mutable
. You can use it to indicate that a particular member of a structure (or class) can be altered even if a particular structure (or class) variable is a const
. For example, consider the following code:
struct data
{
char name[30];
mutable int accesses;
...
};
const data veep = {"Claybourne Clodde", 0, ... };
strcpy(veep.name, "Joye Joux"); // not allowed
veep.accesses++; // allowed
The const
qualifier to veep
prevents a program from changing veep
’s members, but the mutable
specifier to the accesses
member shields accesses
from that restriction.
This book doesn’t use volatile
or mutable
, but there is more to learn about const
.
const
In C++ (but not C), the const
modifier alters the default storage classes slightly. Whereas a global variable has external linkage by default, a const
global variable has internal linkage by default. That is, C++ treats a global const
definition, such as in the following code fragment, as if the static
specifier had been used:
const int fingers = 10; // same as static const int fingers = 10;
int main(void)
{
...
C++ has altered the rules for constant types to make life easier for you. Suppose, for example, that you have a set of constants that you’d like to place in a header file and that you use this header file in several files in the same program. After the preprocessor includes the header file contents in each source file, each source file will contain definitions like this:
const int fingers = 10;
const char * warning = "Wak!";
If global const
declarations had external linkage as regular variables do, this would be an error because of the one definition rule. That is, only one file can contain the preceding declaration, and the other files have to provide reference declarations using the extern
keyword. Moreover, only the declarations without the extern
keyword would be able to initialize values:
// extern would be required if const had external linkage
extern const int fingers; // can't be initialized
extern const char * warning;
So you would need one set of definitions for one file and a different set of declarations for the other files. Instead, because externally defined const
data has internal linkage, you can use the same declarations in all files.
Internal linkage also means that each file gets its own set of constants rather than sharing them. Each definition is private to the file that contains it. This is why it’s a good idea to put constant definitions in a header file. That way, as long as you include the same header file in two source code files, they receive the same set of constants.
If, for some reason, you want to make a constant have external linkage, you can use the extern
keyword to override the default internal linkage:
extern const int states = 50; // definition with external linkage
You then must use the extern
keyword to declare the constant in all files that use the constant. This differs from regular external variables, in which you don’t have to use the keyword extern
when you define a variable, but you use extern
in other files using that variable. Keep in mind, however, now that a single const
is being shared among files, only one file can use initialization.
When you declare a const
within a function or block, it has block scope, which means the constant is usable only when the program is executing code within the block. This means that you can create constants within a function or block and not have to worry about the names conflicting with constants defined elsewhere.
3.145.178.151