std::remove...ove_volatile(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 πŸ¦‹

  1. Removes the topmost const, or the topmost volatile, or both, if present.
  2. Removes the topmost const.
  3. 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 T without 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: removing const/volatile from const volatile int* does not modify the type, because the pointer itself is neither const nor volatile. 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 is const-qualified (since C++11) (class template)
  • is_volatile β€” checks if a type is volatile-qualified (since C++11) (class template)
  • add_cv β€” adds const and/or volatile specifiers to the given type (since C++11) (class template)
  • add_const β€” adds const and/or volatile specifiers to the given type (since C++11) (class template)
  • add_volatile β€” adds const and/or volatile specifiers to the given type (since C++11) (class template)
  • remove_cvref β€” combines std::remove_cv and std::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