std::remove_cv, std::remove_const, std::remove_volatile — cppreference.com
Материал из cppreference.com
<tbody> </tbody>
| Определено в заголовочном файле |
||
|
|
(1) | (начиная с C++11) |
|
|
(2) | (начиная с C++11) |
|
|
(3) | (начиная с C++11) |
Предоставляет определение типа в элементе type, которое такое же как и T, за исключением того, что его самые верхние cv-квалификаторы удалены.
1) Удаляет самый верхний const, самый верхний volatile, или оба, если они есть
2) Удаляет самый верхний const
3) Удаляет самый верхний volatile
Поведение программы, добавляющей специализации для любых шаблонов, описанных на этой странице не определено.
Типы-элементы
| Имя | Определение |
type
|
тип T без cv-квалификатора
|
Вспомогательные типы
<tbody> </tbody>
|
|
(начиная с C++14) | |
|
|
(начиная с C++14) | |
|
|
(начиная с 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() {}