B. Stroustrup - The C++ Programming Language (794319), страница 39
Текст из файла (страница 39)
That is, its type mustbe specified to inform the compiler what kind of entity the name refers to. For example:char ch;string s;auto count = 1;const double pi {3.1415926535897};extern int error_number;const char∗ name = "Njal";const char∗ season[] = { "spring", "summer", "fall", "winter" };vector<string> people { name, "Skarphedin", "Gunnar" };152Types and DeclarationsChapter 6struct Date { int d, m, y; };int day(Date∗ p) { return p−>d; }double sqrt(double);template<class T> T abs(T a) { return a<0 ? −a : a; }constexpr int fac(int n) { return (n<2)?1:n∗fac(n−1); }constexpr double zz { ii∗fac(7) };// possible compile-time evaluation (§2.2.3)// compile-time initializationusing Cmplx = std::complex<double>;struct User;enum class Beer { Carlsberg, Tuborg, Thor };namespace NS { int a; }// type alias (§3.4.5, §6.5)// type nameAs can be seen from these examples, a declaration can do more than simply associate a type with aname.
Most of these declarations are also definitions. A definition is a declaration that supplies allthat is needed in a program for the use of an entity. In particular, if it takes memory to representsomething, that memory is set aside by its definition. A different terminology deems declarationsparts of an interface and definitions parts of an implementation.
When taking that view, we try tocompose interfaces out of declarations that can be replicated in separate files (§15.2.2); definitionsthat set aside memory do not belong in interfaces.Assuming that these declarations are in the global scope (§6.3.4), we have:char ch;auto count = 1;const char∗ name = "Njal";// set aside memory for a char and initialize it to 0// set aside memory for an int initialized to 1// set aside memory for a pointer to char// set aside memory for a string literal "Njal"// initialize the pointer with the address of that string literalstruct Date { int d, m, y; };int day(Date∗ p) { return p−>d; }// Date is a struct with three members// day is a function that executes the specified codeusing Point = std::complex<short>;// Point is a name for std::complex<shor t>Of the declarations above, only three are not also definitions:double sqrt(double);extern int error_number;struct User;// function declaration// variable declaration// type name declarationThat is, if used, the entity they refer to must be defined elsewhere.
For example:double sqrt(double d) { /* ... */ }int error_number = 1;struct User { /* ... */ };There must always be exactly one definition for each name in a C++ program (for the effects of#include, see §15.2.3). However, there can be many declarations.All declarations of an entity must agree on its type. So, this fragment has two errors:int count;int count;// error : redefinitionSection 6.3extern int error_number;extern short error_number;Declarations153// error : type mismatchThis has no errors (for the use of extern, see §15.2):extern int error_number;extern int error_number; // OK: redeclarationSome definitions explicitly specify a ‘‘value’’ for the entities they define.
For example:struct Date { int d, m, y; };using Point = std::complex<short>;int day(Date∗ p) { return p−>d; }const double pi {3.1415926535897};// Point is a name for std::complex<shor t>For types, aliases, templates, functions, and constants, the ‘‘value’’ is permanent.
For non-constdata types, the initial value may be changed later. For example:void f(){int count {1};// initialize count to 1const char∗ name {"Bjarne"}; // name is a variable that points to a constant (§7.5)count = 2;// assign 2 to countname = "Marian";}Of the definitions, only two do not specify values:char ch;string s;See §6.3.5 and §17.3.3 for explanations of how and when a variable is assigned a default value.Any declaration that specifies a value is a definition.6.3.1 The Structure of DeclarationsThe structure of a declaration is defined by the C++ grammar (§iso.A).
This grammar evolved overfour decades, starting with the early C grammars, and is quite complicated. However, without toomany radical simplifications, we can consider a declaration as having five parts (in order):• Optional prefix specifiers (e.g., static or virtual)• A base type (e.g., vector<double> or const int)• A declarator optionally including a name (e.g., p[7], n, or ∗(∗)[])• Optional suffix function specifiers (e.g., const or noexcept)• An optional initializer or function body (e.g., ={7,5,3} or {return x;})Except for function and namespace definitions, a declaration is terminated by a semicolon. Consider a definition of an array of C-style strings:const char∗ kings[] = { "Antigonus", "Seleucus", "Ptolemy" };Here, the base type is const char, the declarator is ∗kings[], and the initializer is the = followed bythe {}-list.A specifier is an initial keyword, such as virtual (§3.2.3, §20.3.2), extern (§15.2), or constexpr(§2.2.3), that specifies some non-type attribute of what is being declared.154Types and DeclarationsChapter 6A declarator is composed of a name and optionally some declarator operators.
The most common declarator operators are:Declarator Operatorsprefixprefixprefixprefixprefixprefixpostfixpostfixpostfix∗∗const∗volatile&&&auto[]()−>pointerconstant pointervolatile pointerlvalue reference (§7.7.1)rvalue reference (§7.7.2)function (using suffix return type)arrayfunctionreturns from functionTheir use would be simple if they were all either prefix or postfix.
However, ∗, [], and () weredesigned to mirror their use in expressions (§10.3). Thus, ∗ is prefix and [] and () are postfix. Thepostfix declarator operators bind tighter than the prefix ones. Consequently, char∗kings[] is an arrayof pointers to char, whereas char(∗kings)[] is a pointer to an array of char.
We have to use parentheses to express types such as ‘‘pointer to array’’ and ‘‘pointer to function’’; see the examples in §7.2.Note that the type cannot be left out of a declaration. For example:const c = 7;// error : no typegt(int a, int b) // error : no return type{return (a>b) ? a : b;}unsigned ui;long li;// OK: ‘‘unsigned’’means ‘‘unsigned int’’// OK: ‘‘long’’ means ‘‘long int’’In this, standard C++ differs from early versions of C and C++ that allowed the first two examplesby considering int to be the type when none was specified (§44.3). This ‘‘implicit int’’ rule was asource of subtle errors and much confusion.Some types have names composed out of multiple keywords, such as long long and volatile int.Some type names don’t even look much like names, such as decltype(f(x)) (the return type of a callf(x); §6.3.6.3).The volatile specifier is described in §41.4.The alignas() specifier is described in §6.2.9.6.3.2 Declaring Multiple NamesIt is possible to declare several names in a single declaration.
The declaration simply contains a listof comma-separated declarators. For example, we can declare two integers like this:int x, y;// int x; int y;Section 6.3.2Declaring Multiple Names155Operators apply to individual names only – and not to any subsequent names in the same declaration.
For example:int∗ p, y;// int* p; int y; NOT int* y;int x, ∗q;// int x; int* q;int v[10], ∗pv; // int v[10]; int* pv;Such declarations with multiple names and nontrivial declarators make a program harder to readand should be avoided.6.3.3 NamesA name (identifier) consists of a sequence of letters and digits. The first character must be a letter.The underscore character, _, is considered a letter. C++ imposes no limit on the number of characters in a name. However, some parts of an implementation are not under the control of the compilerwriter (in particular, the linker), and those parts, unfortunately, sometimes do impose limits.
Somerun-time environments also make it necessary to extend or restrict the set of characters accepted inan identifier. Extensions (e.g., allowing the character $ in a name) yield nonportable programs. AC++ keyword (§6.3.3.1), such as new or int, cannot be used as a name of a user-defined entity.Examples of names are:helloDEFINEDvar0this_is_a_most_unusually_long_identifier_that_is_better_avoidedfoObAru_nameHorseSensevar1CLASS_class___Examples of character sequences that cannot be used as identifiers are:012pay.duea foolfoo˜bar$sys.nameclassif3varNonlocal names starting with an underscore are reserved for special facilities in the implementationand the run-time environment, so such names should not be used in application programs. Similarly, names starting with a double underscore (__) or an underscore followed by an uppercase letter(e.g., _Foo) are reserved (§iso.17.6.4.3).When reading a program, the compiler always looks for the longest string of characters thatcould make up a name.
Hence, var10 is a single name, not the name var followed by the number 10.Also, elseif is a single name, not the keyword else followed by the keyword if.Uppercase and lowercase letters are distinct, so Count and count are different names, but it isoften unwise to choose names that differ only by capitalization. In general, it is best to avoidnames that differ only in subtle ways. For example, in some fonts, the uppercase ‘‘o’’ (O) and zero(0) can be hard to tell apart, as can the lowercase ‘‘L’’ (l), uppercase ‘‘i’’ (I), and one (1).