Correlation between specifier and qualifier?
Asked Answered
P

2

14

const and volatile are called cv-qualifier by the C spec.

What is exactly defference between specifier and qualifier (cv-qualifier)? Is a qualifier is a specifier as well?

Is it necessarry that qualifier is with an lvalue only?

What are qualifiers other than cv-qualifier?

Does my above understanding make any sense?

Prodigy answered 31/8, 2010 at 16:9 Comment(0)
S
6

Most of it doesn't make sense.

Specifier and qualifier are defined in the C++ standard. Qualifier is just an integral part of a specifier. For example, type specifier in a declaration can include cv-qualifiers. I don't see the reason to quote everything from the standard on this topic.

Cv-qualifiers are not restricted to lvalues. Rvalues of class types can also be cv-qualified. It is possible to cv-qualify an rvalue of non-class type, but it will have no effect and will be ignored.

The use of const qualifier that you show in your example with foo is just a syntactic form, which actually means that the const-qualifier is applied to the implied this parameter of the foo method: const A* this. I.e. in this case it does indeed qualify an lvalue, but it is *this, not foo.

The term qualifier also appears in the context of qualified names. Name like some_class::some_member (or some_namespace::some_name) are called qualified names and the some_class:: part is a qualifier.

The idea that if something is an lvalue then you can modify it is totally incorrect. There are modifiable lvalues and non-modifiable lvalues. An object declared as const int i = 5 is an lvalue, yet you can't modify it. Ordinary functions are also lvalues in C++, yet you can't modify a function.

Sewerage answered 31/8, 2010 at 16:24 Comment(2)
+1 for "I don't see the reason to quote everything from the standard on this topic.". Sometimes I wonder whether people think just including some random Standard quote makes their answer less wrong (I'm not talking about this thread, but there are lots of amusing answer threads on SO where that is the case, sadly).Bioastronautics
Hey, someone proved what I claimed yesterday: meta.stackexchange.com/questions/51469/…Bioastronautics
B
6

A cv-qualifier is a specifier, actually a type specifier.

Quoting C++03 7.1:

The specifiers that can be used in a declaration are

decl-specifier:
  storage-class-specifier
  type-specifier
  function-specifier
  friend
  typedef
decl-specifier-seq:
  decl-specifier-seq_opt
  decl-specifier

... while type specifiers are defined:

type-specifier:
  simple-type-specifier
  class-specifier
  enum-specifier
  elaborated-type-specifier
  cv-qualifier

As for the distinction between the word specifier and qualifier:

Each type which is a cv-unqualified complete or incomplete object type or is void has three corresponding cv-qualified versions of its type: a const-qualified version, a volatile-qualified version, and a const-volatile-qualified version. The term object type includes the cv-qualifiers specified when the object is created. The presence of a const specifier in a decl-specifier-seq declares an object of const-qualified object type; such object is called a const object. The presence of a volatile specifier in a decl-specifier-seq declares an object of volatile- qualified object type; such object is called a volatile object. The presence of both cv-qualifiers in a decl-specifier-seq declares an object of const-volatile-qualified object type; such object is called a const volatile object. The cv-qualified or cv-unqualified versions of a type are distinct types; however, they shall have the same representation and alignment requirements.

While it isn't stated obviously, the above paragraph shows the difference. I admit that the definitions could be more strict however.

Borgeson answered 31/8, 2010 at 16:21 Comment(1)
Thanks for the precise reply. In the paragraph you cite I was confused by mixed use of both words: "...The presence of a const specifier in a decl-specifier-seq... ...The presence of a volatile specifier in a decl-specifier-seq... ...The presence of both cv-qualifiers in a decl-specifier-seq...". And as there is no grammar definitions nearby this paragraph, it is not obvious if "const" and "volatile" are specifiers or qualifiers (or qualifier is specific case of specifier or vice-versa). Thanks again.Hallowell
S
6

Most of it doesn't make sense.

Specifier and qualifier are defined in the C++ standard. Qualifier is just an integral part of a specifier. For example, type specifier in a declaration can include cv-qualifiers. I don't see the reason to quote everything from the standard on this topic.

Cv-qualifiers are not restricted to lvalues. Rvalues of class types can also be cv-qualified. It is possible to cv-qualify an rvalue of non-class type, but it will have no effect and will be ignored.

The use of const qualifier that you show in your example with foo is just a syntactic form, which actually means that the const-qualifier is applied to the implied this parameter of the foo method: const A* this. I.e. in this case it does indeed qualify an lvalue, but it is *this, not foo.

The term qualifier also appears in the context of qualified names. Name like some_class::some_member (or some_namespace::some_name) are called qualified names and the some_class:: part is a qualifier.

The idea that if something is an lvalue then you can modify it is totally incorrect. There are modifiable lvalues and non-modifiable lvalues. An object declared as const int i = 5 is an lvalue, yet you can't modify it. Ordinary functions are also lvalues in C++, yet you can't modify a function.

Sewerage answered 31/8, 2010 at 16:24 Comment(2)
+1 for "I don't see the reason to quote everything from the standard on this topic.". Sometimes I wonder whether people think just including some random Standard quote makes their answer less wrong (I'm not talking about this thread, but there are lots of amusing answer threads on SO where that is the case, sadly).Bioastronautics
Hey, someone proved what I claimed yesterday: meta.stackexchange.com/questions/51469/…Bioastronautics

© 2022 - 2024 — McMap. All rights reserved.