std:: remove_cv, std:: remove_const, std:: remove_volatile
From cppreference.net
C++
Metaprogramming library
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<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
,该类型与
T
相同,但其最顶层的 cv 限定符已被移除。
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 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 仅作用于类型,不作用于指针 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 限定符
(类模板) |
|
(C++11)
|
检查类型是否具有 volatile 限定符
(类模板) |
|
(C++11)
(C++11)
(C++11)
|
为给定类型添加
const
和/或
volatile
限定符
(类模板) |
|
(C++20)
|
组合
std::remove_cv
与
std::remove_reference
功能
(类模板) |