What does "cv-unqualified" mean in C++?
Asked Answered
H

6

84

As from subject. I saw this terminology in a question I recently asked, and apparently it's a well established term, but I am not able to find anything on Stack Overflow.

Halophyte answered 14/3, 2013 at 15:12 Comment(3)
@LihO: from what I know about stackoverflow, copypaste is not the preferred method to address a question. It normally involves an extremely detailed set of answers with very specific references to the specsHalophyte
@StefanoBorini: second result from googling cv-qualifiers is clear and exhaustive. Not at all a crappy forum.Martimartial
@Antoine: It's fun how people still assume that what they find in google is what anyone else finds in google. Search results are tailored to your traditional browsing behavior. That is not my second result. Not even first page. In fact, I mostly got Curriculum Vitae unqualified job announcements. (probably because I scouted for jobs six months ago)Halophyte
K
76

There are fundamental types and compound types. Fundamental types are the arithmetic types, void, and std::nullptr_t. Compound types are arrays, functions, pointers, references, classes, unions, enumerations, and pointers to non-static members.

A cv-unqualified type is any of those types.

For any cv-unqualified type, there are three corresponding cv-qualified types:

  • const-qualified - with the const cv-qualifier
  • volatile-qualified - with the volatile cv-qualifier
  • const-volatile-qualified - with both the const and volatile cv-qualifiers

Note, however, that cv-qualifiers applied to an array type actually apply to its elements.

The cv-qualified and cv-unqualified types are distinct. That is int is a distinct type from const int.

Kun answered 14/3, 2013 at 15:21 Comment(4)
wow, I can't understand.. why is it named 'cv-unqualified'? it would have been better if you first told us 'what it means' before 'what it is'. of course I now know cv is for const and volatile..Underexposure
Why pointers, [...], and pointers to non-static members? Isn't the latter included in the former? Or am I missing something?Dispraise
Shouldn't first two paragraphs be removed? What difference it makes if we simply say "A type can be cv-qualified and cv-unqualified."?Alvita
"cv-qualifiers applied to an array type apply to its elements", that's quite a difference from a language like Java, where they apply to the variable itself (instead of the elements)! cppreference also notes this design decision: "array types are considered to have the same cv-qualification as their element types."Dyadic
D
71

A type is "cv-unqualified" if it doesn't have any cv-qualifiers. A cv-qualifer is either const or volatile.

Denary answered 14/3, 2013 at 15:15 Comment(1)
This answers better than the accepted one.Jandel
M
24

cv stands for const and volatile (and more rarely mutable), two attributes qualifying a type. You can manipulate them with std::remove_const and the like in C++11.

The excellent cppreference site gives you more info.

To answer your question, a cv-unqualified type either doesn't have or is stripped from its cv-qualifiers. For instance int is the cv-unqualified part of const volatile int.

std::remove_cv<T>::type is the cv-unqualified partof T.

Martimartial answered 14/3, 2013 at 15:18 Comment(4)
I did, but it's not here. I want it here.Halophyte
But it is! This question for instance is on the same topic but the poster shows he's done some research to understand some basics but is confused by some specific points. Which is I believe the spirit of this site.Martimartial
It is not. The title of the question does not convey its content. I want anyone with the same question as I had go on google and ask "what does cv unqualified mean" and get to this question, where it is clearly explained. On a side note, I think that this piranha-like behavior of the stackoverflow community against reasonable but "perceived easy" questions is really obnoxious, and if there were an alternative, I'd already switched. Calm the damn down peopleHalophyte
@StefanoBorini I just want to mention that searching for cv unqualified C++ brought me here as the first result. Your efforts here to allow this to be the highest search result are very much appreciated. Thanks again!Rhoden
D
13

cv-unqualified type is a type that hasn't been specified by any of cv-qualifiers. These define two basic properties of a type: constness and volatility. See C++03 3.9.3 CV-qualifiers §1:

A type mentioned in 3.9.1 and 3.9.2 is a cv-unqualified type. Each type which is a cv-unqualified complete or incomplete object type or is void (3.9) 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 (1.8) 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 volatilequalified object type; such object is called a volatile object.

The presence of both cv-qualifiers in a declspecifier-seq declares an object of const-volatile-qualified object type; such object is called a const volatile object.

Demiurge answered 14/3, 2013 at 15:26 Comment(0)
A
5

Generally it means "the same type, but with any cv-qualifier removed", so (for example) the cv-unqualified version of void volatile * const x would be void *x.

Note that here, however, I'm removing the cv-qualifiers from both the pointer itself and what it points at. In most cases, cv-unqualified will refer only to one object at a time, so a cv-unqualified version of the pointer itself would still be void volatile *x, whereas a cv-unqualfied version of what it points at would be void *const x.

Autobus answered 14/3, 2013 at 15:16 Comment(0)
A
4

I think First step is to understand possible types & what CV means:

const and volatile appear in any type specifier, including decl-specifier-seq of declaration grammar, to specify constness or volatility of the object being declared or of the type being named.

const - defines that the type is constant.

volatile - defines that the type is volatile.

Explanation

For any type T (including incomplete types), other than function type or reference type, there are three more distinct types in the C++ type system: const-qualified T, volatile-qualified T, and const-volatile-qualified T.

Note: array types are considered to have the same cv-qualification as their element types. When an object is first created, the cv-qualifiers used (which could be part of decl-specifier-seq or part of a declarator in a declaration or part of type-id in a new-expression) determine the constness or volatility of the object, as follows:

const object - an object whose type is const-qualified or a non-mutable subobject of a const object. Such object cannot be modified: attempt to do so directly is a compile-time error, and attempt to do so indirectly (e.g., by modifying the const object through a reference or pointer to non-const type) results in undefined behavior.

volatile object - an object whose type is volatile-qualified, or a subobject of a volatile object, or a mutable subobject of a const-volatile object. Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for the purposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that is sequenced-before or sequenced-after the volatile access. This makes volatile objects suitable for communication with a signal handler, but not with another thread of execution, see std::memory_order). Any attempt to refer to a volatile object through a non-volatile lvalue (e.g. through a reference or pointer to non-volatile type) results in undefined behavior.

const volatile object - an object whose type is const-volatile-qualified, a non-mutable subobject of a const volatile object, a const subobject of a volatile object, or a non-mutable volatile subobject of a const object. Behaves as both a const object and as a volatile object.

Ref: Cpp reference

Aloysia answered 20/5, 2019 at 14:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.