std::remove_cv, std::remove_const, std::remove_volatile
Материал из cppreference.com
Определено в заголовочном файле <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 <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 > // remove_cv работает только с типами, а не с указателями not 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() {}
[править] Смотрите также
(C++11) |
проверяет, является ли тип квалифицированным как const (шаблон класса) |
(C++11) |
проверяет, является ли тип volatile квалифицированным (шаблон класса) |
(C++11)(C++11)(C++11) |
добавляет спецификаторы const и/или volatile к данному типу (шаблон класса) |
(C++20) |
объединяет std::remove_cv и std::remove_reference (шаблон класса) |