std::remove...ove_volatile(3)
π std::remove_cv, std::remove_const, std::remove_volatile
okay bestie, bestie, BESTIE β welcome to the C++ Standard Library page for std::remove_cv, std::remove_const, and std::remove_volatile π this is one of those little <type_traits> icons and honestly? she ate. let's get into it β¨
πΈ NAME
std::remove_cv, std::remove_const, std::remove_volatile β yes, the name is literally just the three of them stacked together, she's not subtle and she doesn't need to be π
β¨ Synopsis
miss thing lives here:
Defined in header <type_traits>
and here's the whole squad, all (since C++11) because they've been iconic for a minute:
template< class T > (1) (since C++11)
struct remove_cv;
template< class T > (2) (since C++11)
struct remove_const;
template< class T > (3) (since C++11)
struct remove_volatile;
so what does she actually do? she provides the member typedef type which is the same as T, except that its topmost cv-qualifiers are removed. that's the whole job. girly pops off the top layer like a sticker π¦
- Removes the topmost
const, or the topmostvolatile, or both, if present. - Removes the topmost
const. - Removes the topmost
volatile.
β οΈ and listen β if the program adds specializations for any of the templates described on this page, the behavior is undefined. do not do that. do not be that girl. π
π Member types
here's the tiny table of truth:
- Name:
type - Definition: the type
Twithout cv-qualifier
π§ Helper types
the glow-up era, (since C++14) β these are the _t versions and they're so much cuter to type fr fr:
template< class T > (since C++14)
using remove_cv_t = typename remove_cv<T>::type;
template< class T > (since C++14)
using remove_const_t = typename remove_const<T>::type;
template< class T > (since C++14)
using remove_volatile_t = typename remove_volatile<T>::type;
π Possible implementation
okay this is the "how would you even build this" section, and honestly it's giving textbook. partial specialization supremacy π
template<class T> struct remove_cv { typedef T type; };
template<class T> struct remove_cv<const T> { typedef T type; };
template<class T> struct remove_cv<volatile T> { typedef T type; };
template<class T> struct remove_cv<const volatile T> { typedef T type; };
template<class T> struct remove_const { typedef T type; };
template<class T> struct remove_const<const T> { typedef T type; };
template<class T> struct remove_volatile { typedef T type; };
template<class T> struct remove_volatile<volatile T> { typedef T type; };
π₯ Example
β οΈ important lore, bestie: removingconst/volatilefromconst volatile int*does not modify the type, because the pointer itself is neitherconstnorvolatile. she only strips the topmost cv-qualifiers. pointers are a different beast and they know it π
// Run this code
#include <type_traits>
template<typename U, typename V>
constexpr bool same = std::is_same_v<U, V>;
static_assert
(
same<std::remove_cv_t<int>, int> &&
same<std::remove_cv_t<const int>, int> &&
same<std::remove_cv_t<volatile int>, int> &&
same<std::remove_cv_t<const volatile int>, int> &&
// remove_cv only works on types, not on pointers
not same<std::remove_cv_t<const volatile int*>, int*> &&
same<std::remove_cv_t<const volatile int*>, const volatile int*> &&
same<std::remove_cv_t<const int* volatile>, const int*> &&
same<std::remove_cv_t<int* const volatile>, int*>
);
int main() {}
π See also
the friend group π
is_constβ checks if a type isconst-qualified (since C++11) (class template)is_volatileβ checks if a type isvolatile-qualified (since C++11) (class template)add_cvβ addsconstand/orvolatilespecifiers to the given type (since C++11) (class template)add_constβ addsconstand/orvolatilespecifiers to the given type (since C++11) (class template)add_volatileβ addsconstand/orvolatilespecifiers to the given type (since C++11) (class template)remove_cvrefβ combinesstd::remove_cvandstd::remove_reference(since C++20) (class template)
and that's the page, bestie! π this one came straight from cppreference.com, dated 2024.06.10, from the C++ Standard Library section β so you know it's the good stuff. go strip some cv-qualifiers, slay responsibly, and remember: topmost only. periodt. βπ«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms