std::remove_cvref
来自 cppreference.cn
定义于头文件 <type_traits> |
||
template< class T > struct remove_cvref; |
(自 C++20 起) | |
如果类型 T
是引用类型,则提供成员 typedef type
,它是 T
引用的类型,并移除了顶层 cv 限定符。否则 type
是 T
,并移除了顶层 cv 限定符。
如果程序为 std::remove_cvref
添加特化,则行为未定义。
内容 |
[编辑] 成员类型
名称 | 定义 |
type
|
T 引用的类型,如果它不是引用,则为 T 本身,并移除了顶层 cv 限定符 |
[编辑] 辅助类型
template< class T > using remove_cvref_t = remove_cvref<T>::type; |
(自 C++20 起) | |
[编辑] 可能的实现
template<class T> struct remove_cvref { using type = std::remove_cv_t<std::remove_reference_t<T>>; }; |
[编辑] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_remove_cvref |
201711L |
(C++20) | std::remove_cvref
|
[编辑] 示例
运行此代码
#include <type_traits> int main() { static_assert(std::is_same_v<std::remove_cvref_t<int>, int>); static_assert(std::is_same_v<std::remove_cvref_t<int&>, int>); static_assert(std::is_same_v<std::remove_cvref_t<int&&>, int>); static_assert(std::is_same_v<std::remove_cvref_t<const int&>, int>); static_assert(std::is_same_v<std::remove_cvref_t<const int[2]>, int[2]>); static_assert(std::is_same_v<std::remove_cvref_t<const int(&)[2]>, int[2]>); static_assert(std::is_same_v<std::remove_cvref_t<int(int)>, int(int)>); }
[编辑] 参见
(C++11)(C++11)(C++11) |
从给定类型移除 const 和/或 volatile 限定符 (类模板) |
(C++11) |
从给定类型移除引用 (类模板) |
(C++11) |
应用类型转换,如同按值传递函数实参时一样 (类模板) |