std::remove_cv, std::remove_const, std::remove_volatile
| 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.
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> 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 << "test1 " << (std::is_same<int, type1>::value ? "passed" : "failed") << '\n'; std::cout << "test2 " << (std::is_same<int, type2>::value ? "passed" : "failed") << '\n'; std::cout << "test3 " << (std::is_same<int, type3>::value ? "passed" : "failed") << '\n'; std::cout << "test4 " << (std::is_same<const volatile int*, type4>::value ? "passed" : "failed") << '\n'; std::cout << "test5 " << (std::is_same<int*, type5>::value ? "passed" : "failed") << '\n'; }
Output:
test1 passed test2 passed test3 passed test4 passed test5 passed
[edit] See also
| (C++11) |
checks if a type is const-qualified (class template) |
| (C++11) |
checks if a type is volatile-qualified (class template) |
| (C++11)(C++11)(C++11) |
adds const or/and volatile specifiers to the given type (class template) |
| (C++20) |
combines std::remove_cv and std::remove_reference (class template) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
