21

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 what is the meaning of "qualifier"?

sth
  • 222,467
  • 53
  • 283
  • 367
ankit
  • 877
  • 3
  • 9
  • 14

1 Answers1

48

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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • 1
    @Alex: * and & are part of the type, rather than a qualification thereto. That's why you can use static_cast<> and const_cast<> to add or remove qualifiers, but you can't convert a T into a T*... they're just fundamentally different things. Similarly, static and extern storage qualifiers don't affect the type of data... just its lifetime and visibility or expected place of definition. – Tony Delroy Sep 24 '10 at 10:43
  • As far i know quallifiers are refering to the preceeding keyword and not as you say "adding the quality to what follows". Teach me if im wrong, but at least about const im pretty sure! – dhein Aug 10 '13 at 01:32
  • @Tony D but the point is if some one thinks it quallifies, what follows he wolud write `char const *var` but it only quallifies what follows, in case there is nothing preceeding. so if u wanc anonst pointer to non const object you had to write `char * const var` and this is whats mostly forgotten ;) just wanted to add. – dhein Aug 17 '13 at 10:26
  • Keywords are not identifiers in the main language grammar, but only for the preprocessor. Preprocesssing tokens and rest-of-the-language tokens are separate entities. – Potatoswatter Nov 18 '13 at 05:02
  • @potatoswatter: "not lexical identifiers" - that's a great and valid point at the lexical level: "identifier" is used in a more restrictive non-keyword sense in the C++11 Standard's formal Grammar, but also used in the more inclusive sense I've used it, e.g. "The identifiers shown in Table 4 are reserved for use as keywords" - clearly reflecting keywords being a subset of identifiers. Still - the restrictive term could be fun: "is that identifier a keyword?" - "what identifier?". – Tony Delroy Nov 18 '13 at 05:31
  • @Potatoswatter: I thought about it some more, and thought this ambiguity of definition should be mentioned in the answer - have updated. Cheers. – Tony Delroy Nov 18 '13 at 06:42