std:: remove_cv, std:: remove_const, std:: remove_volatile
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<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以降) |
メンバー typedef
type
を提供します。これは
T
と同じですが、最上位の cv 修飾子が除去されています。
1)
最上位の
const
または最上位の
volatile
が存在する場合、それらを除去する(両方存在する場合は両方を除去)。
2)
最上位の
const
を削除します。
3)
最上位の
volatile
を削除します。
プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。
目次 |
メンバー型
| 名前 | 定義 |
type
|
CV修飾子なしの型
T
|
ヘルパー型
|
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 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() {}
関連項目
|
(C++11)
|
型がconst修飾されているかどうかをチェックする
(クラステンプレート) |
|
(C++11)
|
型がvolatile修飾されているかどうかをチェックする
(クラステンプレート) |
|
(C++11)
(C++11)
(C++11)
|
指定された型に
const
および/または
volatile
指定子を追加する
(クラステンプレート) |
|
(C++20)
|
std::remove_cv
と
std::remove_reference
を組み合わせる
(クラステンプレート) |