Declarations
A declaration can appear anywhere a statement appears, and certain statements permit additional declarations within those statements.
Declarations made in a substatement (of a selection or loop statement) are limited in scope to the substatement, even if the substatement is not a compound statement. For example, the following statement:
while ( test( ) ) int x = init( );
is equivalent to:
while ( test( ) ) { int x = init( ); }
The first example uses a declaration as the entire loop body, and
the second uses a compound statement (enclosing the loop body in curly
braces). In both cases, though, the scope of x
is limited to the body of the while
loop.
Declaration Statements
A simple declaration can appear wherever a statement can be
used. You can declare an object, a type, or a namespace alias. You can
also write a using
declaration or
using
directive. You can declare a
function, but not define a function, although there is rarely any
reason to declare a function locally. You cannot define a namespace or
declare a template.
In traditional C programming, declarations appear at the start of each block or compound statement. In C++ (and in the C99 standard), declarations can appear anywhere a statement can, which means you can declare variables close to where they are used. Example 4-1 shows examples of how declarations can be mixed with statements.
#include <cctype> #include <cstddef> #include <iomanip> #include <iostream> #include <ostream> #include <string> // Count lines, words, and characters in the standard input. int main( ) {unsigned long num_lines, num_words, num_chars;
num_lines = num_words = num_chars = 0;using namespace std; string line;
while (getline(cin, line)) { ++num_lines; num_chars += line.length( ) + 1;bool in_word = false;
for (size_t i = 0; char c = line[i]; ++i) if (isspace(static_cast<unsigned char>(c))) { if (in_word) ++num_words; in_word = false; } else if (! in_word) in_word = true; if (in_word) ++num_words; } cout << right << setw(10) << num_lines << setw(10) << num_words << setw(10) << num_chars << '\n'; }
Sometimes a construct can look like an expression statement or a declaration. These ambiguities are resolved in favor of declarations. Example 4-2 shows some declarations that look like they might be expressions.
class cls { public: cls( ); cls(int x); }; int x; int* y = &x; int main( ) { // The following are unambiguously expressions, constructing instances of cls.cls(int(x)); cls(*static_cast<int*>(y));
// Without the redundant casts, though, they would look like declarations, not // expressions.cls(x);
// Declares a variable xcls(*y);
// Declares a pointer y }
Condition Declarations
The for
, if
, switch
, and while
statements permit a declaration within each statement’s condition:
if (int x = test_this(a, b)) { cout << x; }
If the condition contains a declaration, the scope of the
declared name extends to the end of the entire statement. In
particular, a name declared in the condition of an if
statement is visible in the else
part of the statement. In a loop, the
condition object is created and destroyed for each iteration of the
loop. The name cannot be redeclared in the immediate substatement (but
can be redeclared in a nested statement). The name is not visible in
subsequent statements. For example:
if (derived* d = dynamic_cast<derived*>(b)) { d->derived_only_func( ); } else { assert(d == NULL); // Same d as above double d; // Error: can't redeclare d if (d == 0) int d; // Valid: inner block } cout << d; // Invalid: d no longer in scope
Like the if
, switch
, and
while
statements, a for
loop permits a declaration in its
condition. Unlike those other statements, it also allows a declaration
to initialize the loop. Both declarations are in the same scope. See
Section 4.5.2 for
details.
The syntax for a condition declaration is:
type-specifiers
declarator
=expression
This syntax is similar to the syntax of a simple object declaration. In this case, the initializer is required (without it, the condition would not have a value), and only one declarator is permitted. See Chapter 2 for more information about type specifiers and declarators.
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.