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

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< class T >
struct remove_cv;
(1) (C++11 feature)
template< class T >
struct remove_const;
(2) (C++11 feature)
template< class T >
struct remove_volatile;
(3) (C++11 feature)

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

1) removes the topmost const, the topmost volatile, or both, if present.

2) removes the topmost const

3) removes the topmost volatile

Contents

[edit] Member types

Name Definition
type the type T without cv-qualifier

[edit] Equivalent definition

[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>
#include <typeinfo>
 
int main() {
    typedef std::remove_cv<const int>::type type1;
    typedef std::remove_cv<volatile int>::type type2;
    typedef std::remove_cv<const volatile int>::type type3;
    typedef std::remove_cv<const volatile int*>::type type4;
    typedef std::remove_cv<int * const volatile>::type type5;
 
    std::cout << (std::is_same<int, type1>::value
        ? "test1 passed"
        : "test1 failed") << '\n';
    std::cout << (std::is_same<int, type2>::value
        ? "test2 passed"
        : "test2 failed") << '\n';
    std::cout << (std::is_same<int, type3>::value
        ? "test3 passed"
        : "test3 failed") << '\n';
    std::cout << (std::is_same<const volatile int*, type4>::value
        ? "test4 passed"
        : "test4 failed") << '\n';
    std::cout << (std::is_same<int*, type5>::value
        ? "test5 passed"
        : "test5 failed") << '\n';
}

Output:

test1 passed
test2 passed
test3 passed
test4 passed
test5 passed

[edit] See also

is_const (C++11)
checks if a type is const-qualified
(class template)
is_volatile (C++11)
checks if a type is volatile-qualified
(class template)