std::remove_cv, std::remove_const, std::remove_volatile
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <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
|
ヘルパー型
<tbody> </tbody> 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 の除去は型を変更しません。
Run this code
#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 修飾されているかどうか調べます (クラステンプレート) |
(C++11) |
型が volatile 修飾されているかどうか調べます (クラステンプレート) |
(C++11)(C++11)(C++11) |
指定された型に const, volatile またはその両方の指定子を追加します (クラステンプレート) |
(C++20) |
std::remove_cv と std::remove_reference を合わせたもの (クラステンプレート) |