Namespaces
Variants

std:: remove_cvref

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
remove_cvref
(C++20)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T >
struct remove_cvref ;
(C++20 起)

如果类型 T 是引用类型,则提供成员类型定义 type ,该类型为 T 所引用类型且移除其最顶层的 cv 限定符。否则 type 为移除最顶层 cv 限定符后的 T 类型。

如果程序为 std::remove_cvref 添加特化,则行为是未定义的。

目录

成员类型

名称 定义
type 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)>);
}

参见

移除给定类型中的 const 和/或 volatile 限定符
(类模板)
移除给定类型的引用
(类模板)
(C++11)
应用按值传递函数参数时的类型转换
(类模板)