C++标准模板(STL)- 类型支持 (杂项变换,将 std::remove_cv 与 std::remove_reference 结合,std::remove_cvref)

类型特性

类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
 

杂项变换

将 std::remove_cv 与 std::remove_reference 结合

std::remove_cvref

template< class T >
struct remove_cvref;

(C++20 起)

若类型 T 为引用类型,则提供成员 type ,它是移除了其最顶层 cv 限定符的 T 所引用的类型。否则 type 为移除最顶层 cv 限定符的 T

成员类型

名称 定义
type T 所引用的类型,或若 T 不是引用则为其自身,移除顶层 cv 限定符

辅助类型

template< class T >
using remove_cvref_t = typename remove_cvref<T>::type;

(C++20 起)

可能的实现

namespace std
{
template< class T >
struct remove_cvref
{
    typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
};
}

调用示例

#include <iostream>
#include <type_traits>
#include <typeinfo>

namespace std
{
template< class T >
struct remove_cvref
{
    typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
};
}

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_same<std::remove_cvref<int>::type, int>():                  "
              << std::is_same<std::remove_cvref<int>::type, int>() << std::endl;
    std::cout << "std::is_same<std::remove_cvref<const int>::type, int>():            "
              << std::is_same<std::remove_cvref<const int>::type, int>() << std::endl;
    std::cout << "std::is_same<std::remove_cvref<int&>::type, int>():                 "
              << std::is_same<std::remove_cvref<int&>::type, int>() << std::endl;
    std::cout << "std::is_same<std::remove_cvref<int&&>::type, int> ():               "
              << std::is_same < std::remove_cvref < int&& >::type, int > () << std::endl;
    std::cout << "std::is_same<std::remove_cvref<const int&>::type, int>():           "
              << std::is_same<std::remove_cvref<const int&>::type, int>::value << std::endl;
    std::cout << "std::is_same<std::remove_cvref<const int[2]>::type, int[2]>():      "
              << std::is_same<std::remove_cvref<const int[2]>::type, int[2]>() << std::endl;
    std::cout << "std::is_same<std::remove_cvref<const int(&)[2]>::type, int[2]>():   "
              << std::is_same<std::remove_cvref<const int(&)[2]>::type, int[2]>() << std::endl;
    std::cout << "std::is_same<std::remove_cvref<int(int)>::type, int(int)>():        "
              << std::is_same<std::remove_cvref<int(int)>::type, int(int)>() << std::endl;

    return 0;
}

输出

std::is_same<std::remove_cvref<int>::type, int>():                  true
std::is_same<std::remove_cvref<const int>::type, int>():            true
std::is_same<std::remove_cvref<int&>::type, int>():                 true
std::is_same<std::remove_cvref<int&&>::type, int> ():               true
std::is_same<std::remove_cvref<const int&>::type, int>():           true
std::is_same<std::remove_cvref<const int[2]>::type, int[2]>():      true
std::is_same<std::remove_cvref<const int(&)[2]>::type, int[2]>():   true
std::is_same<std::remove_cvref<int(int)>::type, int(int)>():        true

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/134749210