std::remove_cv, std::remove_const, std::remove_volatile
From 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 というメンバtypedefを提供します。これは、Tから最上位のcv修飾子を取り除いたものと同じ型です。
1) 存在する場合、最上位の const、または最上位の volatile、またはその両方を取り除きます。
2) 最上位の const を取り除きます。
3) 最上位の volatile を取り除きます。
プログラムがこのページで説明されているテンプレートのいずれかに特殊化を追加する場合、動作は未定義です。
目次 |
[編集] Member types
| 名前 | 定義 |
type
|
cv修飾子なしの型 T |
[編集] Helper types
| 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以降) | |
[編集] 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; }; |
[編集] Example
const volatile int* からconst/volatileを削除しても、ポインタ自体が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() {}
[編集] See also
| (C++11) |
型がconst修飾されているかをチェックする (クラステンプレート) |
| (C++11) |
型がvolatile修飾されているかをチェックする (クラステンプレート) |
| (C++11)(C++11)(C++11) |
与えられた型に const および/または volatile 修飾子を追加する (クラステンプレート) |
| (C++20) |
std::remove_cv と std::remove_reference を組み合わせます (クラステンプレート) |