std::remove_cv, std::remove_const, std::remove_volatile - cppreference.com
提供: cppreference.com
<tbody> </tbody>
| ヘッダ |
||
|
|
(1) | (C++11以上) |
|
|
(2) | (C++11以上) |
|
|
(3) | (C++11以上) |
最上段の cv 修飾が除去されることを除いて T と同じ型であるメンバ型 type が提供されます。
1) 最上段の const、最上段の volatile、またはその両方 (もしあれば) を除去します。
2) 最上段の const を除去します。
3) 最上段の volatile を除去します。
このページで説明しているテンプレート に対して特殊化を追加するプログラムは未定義です。
メンバ型
| 名前 | 定義 |
type
|
cv 修飾の除去された型 T
|
ヘルパー型
<tbody> </tbody>
|
|
(C++14以上) | |
|
|
(C++14以上) | |
|
|
(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