Namespaces
Variants

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

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
remove_cv remove_const remove_volatile
(C++11) (C++11) (C++11)
(C++11) (C++11) (C++11)
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダーで定義 <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 を削除します。

プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - C++関連の専門用語(Member types、Helper types、Possible implementation、Example、See also)は翻訳せずにそのまま保持しました - HTMLタグ、属性、クラス名、IDなどは完全に保持されています - 数値やリンクなどの書式も元のまま維持されています

メンバー型

名前 定義
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; };
**注記**: 提供されたHTMLコンテンツはすべてC++コードブロック内に含まれており、指定された条件(HTMLタグ・属性の非翻訳、 /
/タグ内テキストの保持、C++専門用語の非翻訳)に基づき、翻訳対象となる自然言語テキストは存在しませんでした。上記は元のコンテンツを完全に保持した出力となります。

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修飾されているかどうかをチェックする
(クラステンプレート)
型がvolatile修飾されているかどうかをチェックする
(クラステンプレート)
(C++11) (C++11) (C++11)
指定された型に const および/または volatile 指定子を追加する
(クラステンプレート)
std::remove_cv std::remove_reference を組み合わせる
(クラステンプレート)