名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | types
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(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++11)
(C++17)
(C++11)(C++20未満)(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以上)

最上段の cv 修飾が除去されることを除いて T と同じ型であるメンバ型 type が提供されます。

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 でもないため、 const volatile int * からの const/volatile の除去は型を変更しません。

#include <iostream>
#include <type_traits>
 
int main() {
    typedef std::remove_cv<const int>::type type1;
    typedef std::remove_cv<volatile int>::type type2;
    typedef std::remove_cv<const volatile int>::type type3;
    typedef std::remove_cv<const volatile int*>::type type4;
    typedef std::remove_cv<int * const volatile>::type type5;
 
    std::cout << "test1 " << (std::is_same<int, type1>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test2 " << (std::is_same<int, type2>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test3 " << (std::is_same<int, type3>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test4 " << (std::is_same<const volatile int*, type4>::value
        ? "passed" : "failed") << '\n';
    std::cout << "test5 " << (std::is_same<int*, type5>::value
        ? "passed" : "failed") << '\n';
}

出力:

test1 passed
test2 passed
test3 passed
test4 passed
test5 passed

[編集] 関連項目

(C++11)
型が const 修飾されているかどうか調べます
(クラステンプレート) [edit]
型が volatile 修飾されているかどうか調べます
(クラステンプレート) [edit]
(C++11)(C++11)(C++11)
指定された型に const, volatile またはその両方の指定子を追加します
(クラステンプレート) [edit]
std::remove_cvstd::remove_reference を合わせたもの
(クラステンプレート) [edit]