Correlation between specifier and qualifier?

喜欢而已 提交于 2019-11-29 17:45:48

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3610943/correlation-between-specifier-and-qualifier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!