名前空間
変種
操作

std::remove_cv, std::remove_const, std::remove_volatile

From cppreference.com
< cpp‎ | types
 
 
メタプログラミングライブラリ
型特性
型のカテゴリ
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型のプロパティ
(C++11)
(C++11)
(C++14)
(C++11)(C++26で非推奨)
(C++11)(C++20まで*)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
サポートされている操作
関係とプロパティクエリ
型の変更
remove_cvremove_constremove_volatile
(C++11)(C++11)(C++11)
(C++11)(C++11)(C++11)
型の変換
(C++11)(C++23で非推奨)
(C++11)(C++23で非推奨)
(C++11)
(C++11)(C++20まで*)(C++17)

(C++11)
(C++17)
コンパイル時有理数演算
コンパイル時整数シーケンス
 
ヘッダ <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修飾されているかをチェックする
(クラステンプレート) [編集]
型がvolatile修飾されているかをチェックする
(クラステンプレート) [編集]
(C++11)(C++11)(C++11)
与えられた型に const および/または volatile 修飾子を追加する
(クラステンプレート) [編集]
std::remove_cvstd::remove_reference を組み合わせます
(クラステンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)