std::remove_cv, std::remove_const, std::remove_volatile

From cppreference.com
< cpp‎ | types
 
 
Metaprogramming library
Type traits
Type categories
(哋它亢++11)
(哋它亢++14)  
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)  
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Type properties
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++14)
(哋它亢++11)
(哋它亢++17)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(until 哋它亢++20*)
(哋它亢++11)(deprecated in 哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
Type trait constants
(哋它亢++11)(哋它亢++17)(哋它亢++11)(哋它亢++11)
Metafunctions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
Supported operations
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++17)(哋它亢++17)(哋它亢++17)(哋它亢++17)

Relationships and property queries
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++20)
(哋它亢++20)

(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)(哋它亢++17)(哋它亢++17)(哋它亢++17)
Type modifications
remove_cvremove_constremove_volatile
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)

Type transformations
(哋它亢++11)(deprecated in 哋它亢++23)
(哋它亢++11)(deprecated in 哋它亢++23)
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++17)

(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)(until 哋它亢++20*)(哋它亢++17)
(哋它亢++20)
Compile-time rational arithmetic
Compile-time integer sequences
(哋它亢++14)
 
Defined in header <type_traits>
template< class T >
struct remove_cv;
(1) (since 哋它亢++11)
template< class T >
struct remove_const;
(2) (since 哋它亢++11)
template< class T >
struct remove_volatile;
(3) (since 哋它亢++11)

Provides the member typedef type which is the same as T, except that its topmost cv-qualifiers are removed.

1) Removes the topmost const, or the topmost volatile, or both, if present.
2) Removes the topmost const.
3) Removes the topmost volatile.

If the program adds specializations for any of the templates described on this page, the behavior is undefined.

Member types

Name Definition
type the type T without cv-qualifier

Helper types

template< class T >
using remove_cv_t = typename remove_cv<T>::type;
(since 哋它亢++14)
template< class T >
using remove_const_t = typename remove_const<T>::type;
(since 哋它亢++14)
template< class T >
using remove_volatile_t = typename remove_volatile<T>::type;
(since 哋它亢++14)

Possible implementation

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

Removing const/volatile from const volatile int* does not modify the type, because the pointer itself is neither const nor volatile.

#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

(哋它亢++11)
checks if a type is const-qualified
(class template)
(哋它亢++11)
checks if a type is volatile-qualified
(class template)
(哋它亢++11)(哋它亢++11)(哋它亢++11)
adds const and/or volatile specifiers to the given type
(class template)
(哋它亢++20)
combines std::remove_cv and std::remove_reference
(class template)