std::remove_cv, std::remove_const, std::remove_volatile
Определено в заголовке <type_traits> | ||
|---|---|---|
template< class T > struct remove_cv; | (1) | (с C++11) |
template< class T > struct remove_const; | (2) | (с C++11) |
template< class T > struct remove_volatile; | (3) | (с C++11) |
Предоставляет тип-член type, который эквивалентен T, за исключением того, что его самые внешние квалификаторы cv удаляются.
1) Удаляет самый внешний
const, или самый внешний volatile, или оба, если они присутствуют.
2) Удаляет самый внешний
const.
3) Удаляет самый внешний
volatile.Поведение программы, добавляющей специализации для любого из шаблонов, описанных на этой странице, не определено.
Типы-члены
| Название | Определение |
|---|---|
type | тип T без квалификатора cv |
Вспомогательные типы
template< class T > using remove_cv_t = typename remove_cv<T>::type; | (с C++14) | |
template< class T > using remove_const_t = typename remove_const<T>::type; | (с C++14) | |
template< class T > using remove_volatile_t = typename remove_volatile<T>::type; | (с C++14) |
Возможная реализация
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; }; |
Пример
Удаление const/volatile из const volatile int* не изменяет тип, потому что сам указатель не является ни const, ни 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() {}См. также
|
(C++11) | проверяет, является ли тип const-квалифицированным (шаблон класса) |
|
(C++11) | проверяет, является ли тип volatile-квалифицированным (шаблон класса) |
|
(C++11)(C++11)(C++11) | добавляет спецификаторы const и/или volatile к заданному типу (шаблон класса) |
|
(C++20) | объединяет std::remove_cv и std::remove_reference (шаблон класса) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/remove_cv