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
的特化,则行为未定义。
目录 |
[编辑] 成员类型
名称 | 定义 |
类型
|
如果 T 不是引用,则为 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>>; }; |
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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) |
当函数参数按值传递时应用类型转换 (类模板) |