开发者

What is the meaning of "qualifier"?

开发者 https://www.devze.com 2023-01-17 05:08 出处:网络
What is the meaning of \"qualifier\" and the difference between \"qualifier\" and \"keyword\"? For the volatile qualifier in C and we can say that volatile is a keyword, so 开发者_StackOverflowwhat i

What is the meaning of "qualifier" and the difference between "qualifier" and "keyword"?

For the volatile qualifier in C and we can say that volatile is a keyword, so 开发者_StackOverflowwhat is the meaning of "qualifier"?


A qualifier adds an extra "quality", such as specifying volatility or constness of a variable. They're similar to adjectives: "a fickle man", "a volatile int", "an incorruptible lady", "a const double". With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored.

keywords are predefined reserved identifiers (arguably, see below) that the language itself assigns some meaning to, rather than leaving free for you to use for your own purposes (i.e. naming your variables, types, namespaces, functions...).

Examples

  • volatile and const are both qualifiers and keywords
  • if, class, namespace are keywords but not qualifiers
  • std, main, iostream, x, my_counter are all identifiers but neither keywords nor qualifiers

There's a full list of keywords at http://www.cppreference.com/wiki/keywords/start. C++ doesn't currently have any qualifiers that aren't keywords (i.e. they're all "words" rather than some punctuation symbols).


Where do qualifiers appear relative to other type information?

A quick aside from "what does qualifier mean" into the syntax of using a qualifier - as Zaibis comments below:

...[qualifiers] only qualify what follows [when] there is nothing preceding. so if you want a const pointer to non-const object you had to write char * const var...

 


A bit (lot?) about identifiers

identifiers themselves are lexical tokens (distinct parts of the C++ source code) that:

  • begin with a alpha/letter character or underscore
  • continue with 0 or more alphanumerics or underscores

If it helps, you can think of identifiers as specified by the regexp "[A-Za-z_][A-Za-z_0-9]*". Examples are "egg", "string", "__f", "x0" but not "4e4" (a double literal), "0x0a" (that's a hex literal), "(f)" (that's three lexical tokens, the middle being the identifier "f").

    But are keywords identifiers?

For C++, the terminology isn't used consistently. In general computing usage, keywords are a subset of identifiers, and some places/uses in the C++11 Standard clearly reflect that:

  • "The identifiers shown in Table 4 are reserved for use as keywords" (first sentence in 2.12 Keywords)
  • "Identifiers that are keywords or operators in C++..." (from 17.6.1.2 footnote 7)

(There are alternative forms of some operators - not, and, xor, or - though annoyingly Visual C++ disables them by default to avoid breaking old code that used them but not as operators.)

As Potatoswatter points out in a comment, in many other places the Standard defines lexical tokens identifier and keyword as mutually exclusive tokens in the Grammar:

  • "There are five kinds of tokens: identifiers, keywords, ..." (2.7 Tokens)

There's also an edge case where the determination's context sensitive:

  • If a keyword (2.12) or an alternative token (2.6) that satisfies the syntactic requirements of an identifier (2.11) is contained in an attribute-token, it is considered an identifier. (7.6.1. Attribute Syntax and Semantics 2)

Non-keyword identifiers you still shouldn't use

Some identifiers, like "std" or "string", have a specific usage specified in the C++ Standard - they are not keywords though. Generally, the compiler itself doesn't treat them any differently to your own code, and if you don't include any Standard-specified headers then the compiler probably won't even know about the Standard-mandated use of "std". You might be able to create your own function, variable or type called "std". Not a good idea though... while it's nice to understand the general division between keywords and the Standard library, implementations have freedom to blur the boundaries so you should just assume C++ features work when relevant headers are included and your usage matches documentation, and not do anything that might conflict.

0

精彩评论

暂无评论...
验证码 换一张
取 消