std::remove_cvref
来自cppreference.com
| 定义于头文件 <type_traits>
|
||
| template< class T > struct remove_cvref; |
(C++20 起) | |
若类型 T 为引用类型,则提供成员 type ,它是移除了其最顶层 cv 限定符的 T 所引用的类型。否则 type 为移除最顶层 cv 限定符的 T 。
添加 remove_cvref 的特化的程序行为未定义。
成员类型
| 名称 | 定义 |
type
|
T 所引用的类型,或若 T 不是引用则为其自身,移除顶层 cv 限定符
|
辅助类型
| template< class T > using remove_cvref_t = typename remove_cvref<T>::type; |
(C++20 起) | |
可能的实现
template< class T > struct remove_cvref { typedef std::remove_cv_t<std::remove_reference_t<T>> type; }; |
示例
运行此代码
#include <iostream> #include <type_traits> int main() { std::cout << std::boolalpha << std::is_same_v<std::remove_cvref_t<int>, int> << '\n' << std::is_same_v<std::remove_cvref_t<int&>, int> << '\n' << std::is_same_v<std::remove_cvref_t<int&&>, int> << '\n' << std::is_same_v<std::remove_cvref_t<const int&>, int> << '\n' << std::is_same_v<std::remove_cvref_t<const int[2]>, int[2]> << '\n' << std::is_same_v<std::remove_cvref_t<const int(&)[2]>, int[2]> << '\n' << std::is_same_v<std::remove_cvref_t<int(int)>, int(int)> << '\n'; }
输出:
true true true true true true true
参阅
| (C++11)(C++11)(C++11) |
从给定类型移除 const 或/与 volatile 限定符 (类模板) |
| (C++11) |
从给定类型移除引用 (类模板) |
| (C++11) |
实施当按值传递实参给函数时所进行的类型变换 (类模板) |