Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | types
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)
Stacktrace
 
Type support
Basic types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)

(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
remove_cvremove_constremove_volatile
(C++11)(C++11)(C++11)
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)

(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template< class T >
struct remove_cv;
(1) (since C++11)
template< class T >
struct remove_const;
(2) (since C++11)
template< class T >
struct remove_volatile;
(3) (since C++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

The behavior of a program that adds specializations for any of the templates described on this page is undefined.

Contents

[edit] Member types

Name Definition
type the type T without cv-qualifier

[edit] Helper types

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

[edit] 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; };

[edit] Example

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

#include <iostream>
#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 >
    and same< std::remove_cv_t< const int >, int >
    and same< std::remove_cv_t< volatile int >, int >
    and same< std::remove_cv_t< const volatile int >, int >
    and same< std::remove_cv_t< const volatile int* >, const volatile int* >
    and same< std::remove_cv_t< const int* volatile >, const int* >
    and same< std::remove_cv_t< int* const volatile >, int* >
);
 
int main() {
    std::cout << std::boolalpha;
 
    using type1 = std::remove_cv<const int>::type;
    using type2 = std::remove_cv<volatile int>::type;
    using type3 = std::remove_cv<const volatile int>::type;
    using type4 = std::remove_cv<const volatile int*>::type;
    using type5 = std::remove_cv<int* const volatile>::type;
 
    std::cout << std::is_same<type1, int>::value << "\n";
    std::cout << std::is_same<type2, int>::value << "\n";
    std::cout << std::is_same<type3, int>::value << "\n";
    std::cout << std::is_same<type4, int*>::value << " "
              << std::is_same<type4, const volatile int*>::value << "\n";
    std::cout << std::is_same<type5, int*>::value << "\n";
}

Output:

true
true
true
false true
true

[edit] See also

(C++11)
checks if a type is const-qualified
(class template) [edit]
checks if a type is volatile-qualified
(class template) [edit]
(C++11)(C++11)(C++11)
adds const or/and volatile specifiers to the given type
(class template) [edit]
combines std::remove_cv and std::remove_reference
(class template) [edit]