词条 | Type qualifier |
释义 |
In the C, C++, and D programming languages, a type qualifier is a keyword that is applied to a type, resulting in a qualified type. For example, Type qualifiers are a way of expressing additional information about a value through the type system, and ensuring correctness in the use of the data. Type qualifiers are not generally used outside the C/C++ family of languages: many languages have a notion of constants, but express this by the name binding being constant (a "variable that doesn't vary"), rather than through the type system; see alternatives, below. By languageC/C++{{as of|2014}} and C11, there are four type qualifiers in standard C:const (C89), volatile (C89), restrict (C99) and _Atomic (C11) – the latter has a private name to avoid clashing with user-defined names.[1] The first two of these, const and volatile , are also present in C++, and are the only type qualifiers in C++. Thus in C++ the term "cv-qualified type" (for const and volatile) is often used for "qualified type", while the terms "c-qualified type" and "v-qualified type" are used when only one of the qualifiers is relevant.Of these, DIn D the type constructors are Syntax{{see also|C syntax#Type qualifiers}}In C and C++, a type is given in a function declaration or variable declaration by giving one or more type specifiers, and optionally type qualifiers. For example, an integer variable can be declared as: where where both where Variable declarations further have an optional storage class specifier. Properly this is a separate topic, distinct from the type, though Volatile-correctness{{See also|Const-correctness}}The other qualifier in C and C++, The qualifier is most often found in code that manipulates hardware directly (such as in embedded systems and device drivers) and in multithreaded applications (though often used incorrectly in that context; see external links at volatile variable). It can be used in exactly the same manner as Because HistoryThe notion of a type qualifier was introduced, along with the example of "It served two functions: as a way of defining a symbolic constant that obeys scope and type rules (that is, without using a macro) and as a way of deeming an object in memory immutable."
Java does not have type qualifiers, and conspicuously omitted Alternatives{{see also|const-correctness}}Other languages take a different approach, considering constancy a property of an identifier (or name binding), not a type. Such languages thus have constant identifiers (corresponding to "variables" that do not vary) with single assignment, but do not have a notion of const-correctness: since constancy is not part of the type, there is no possibility of type mismatch. Examples include Ada 83 with constant objects and a Notes{{notelist}}References1. ^C11:The New C Standard, Thomas Plum 2. ^"Generic 3. ^Bjarne Stroustrup, "Extensions of the C Language Type Concept.", Bell Labs internal Technical Memorandum, January 5, 1981. 4. ^1 Sibling Rivalry: C and C++, Bjarne Stroustrup, 2002, p. 5 5. ^Dennis M. Ritchie, "The Development of the C Language {{Webarchive|url=https://archive.is/20150110214721/http://cm.bell-labs.com/who/dmr/chist.html |date=2015-01-10 }}", 2003: "X3J11 also introduced a host of smaller additions and adjustments, for example, the type qualifiers const and volatile, and slightly different type promotion rules." 6. ^It appears in the notes for the European UNIX System User Group (EUUC) meeting technical talk "The ANSI Draft Standard for the C Programming Language" by Mike Banahan, 1985 September 13, as printed in the Australian Unix systems User Group Newsletter (AUUGN), Vol 6, No 6, p. 73 7. ^{{cite newsgroup|url=http://yarchive.net/comp/volatile.html|author=John Mashey|newsgroup=comp.arch|title=Re: RISC vs CISC? Call a spade a spade?|message-id=7037@spim.mips.COM|date=16 Aug 1991}} 8. ^"Let me begin by saying that I'm not convinced that even the pre-December qualifiers ('const' and 'volatile') carry their weight; I suspect that what they add to the cost of learning and using the language is not repaid in greater expressiveness. 'Volatile', in particular, is a frill for esoteric applications, and much better expressed by other means. Its chief virtue is that nearly everyone can forget about it. 'Const' is simultaneously more useful and more obtrusive; you can't avoid learning about it, because of its presence in the library interface. Nevertheless, I don't argue for the extirpation of qualifiers, if only because it is too late." 9. ^JDK-4211070: Java should support const parameters (like C++) for code {{sic|nolink=y|maint|ainence|expected=maintenance}} 10. ^1815A, 3.2.1. Object Declarations: "The declared object is a constant if the reserved word constant appears in the object declaration; the declaration must then include an explicit initialization. The value of a constant cannot be modified after initialization. Formal parameters of mode in of subprograms and entries, and generic formal parameters of mode in, are also constants; a loop parameter is a constant within the corresponding loop; a subcomponent or slice of a constant is a constant." External links
2 : C (programming language)|C++ |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。