C++标准模板(STL)- 类型支持 (类型特性,is_pointer,is_lvalue_reference,is_rvalue_reference)

类型特性

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

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

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

类型属性

定义于头文件 <type_traits>

基础类型类别

继承自 std::integral_constant

成员常量

|--------------|------------------------------------------|
| value [静态] | 若 T 为指针类型则为 true ,否则为 false (公开静态成员常量) |

成员函数

|--------------------|---------------------------------|
| operator bool | 转换对象为 bool ,返回 value (公开成员函数) |
| operator() (C++14) | 返回 value (公开成员函数) |

成员类型

|--------------|---------------------------------------|
| 类型 | 定义 |
| value_type | bool |
| type | std::integral_constant<bool, value> |

检查类型是否为指针类型

std::is_pointer

|------------------------------------------|---|-----------|
| template< class T > struct is_pointer; | | (C++11 起) |

检查 T 是否为指向对象指针或指向函数指针(但不是指向成员/成员函数指针)。若 T 是对象/函数指针类型,则提供等于 true 的成员常量 value 。否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_pointer_v = is_pointer<T>::value; | | (C++17 起) |

可能的实现
template< class T > struct is_pointer_helper     : std::false_type {};
template< class T > struct is_pointer_helper<T*> : std::true_type {};
template< class T > struct is_pointer :
is_pointer_helper<typename std::remove_cv<T>::type> {};

检查类型是否为左值引用

std::is_lvalue_reference

|---------------------------------------------------|---|-----------|
| template< class T > struct is_lvalue_reference; | | (C++11 起) |

检查 T 是否为左值引用类型。若 T 是左值引用类型,则提供等于 true 的成员常量 value ,否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|------------------------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_lvalue_reference_v = is_lvalue_reference<T>::value; | | (C++17 起) |

可能的实现
template<class T> struct is_lvalue_reference     : std::false_type {};
template<class T> struct is_lvalue_reference<T&> : std::true_type {};

检查类型是否为右值引用

std::is_rvalue_reference

|---------------------------------------------------|---|-----------|
| template< class T > struct is_rvalue_reference; | | (C++11 起) |

检查 T 是否为右值引用类型。若 T 是右值引用类型,则提供等于 true 的成员常量 value ,否则, value 等于 false 。

模板形参

|---|---|--------|
| T | - | 要检查的类型 |

辅助变量模板

|------------------------------------------------------------------------------------------------------|---|-----------|
| template< class T > inline constexpr bool is_rvalue_reference_v = is_rvalue_reference<T>::value; | | (C++17 起) |

可能的实现
template <class T> struct is_rvalue_reference      : std::false_type {};
template <class T> struct is_rvalue_reference<T&&> : std::true_type {};

调用示例

#include <iostream>
#include <type_traits>

class A {};

int main()
{
    std::cout << std::boolalpha;
    std::cout << "std::is_pointer<A>::value:            "
              << std::is_pointer<A>::value << std::endl;
    std::cout << "std::is_pointer<A *>::value:          "
              << std::is_pointer<A *>::value << std::endl;
    std::cout << "std::is_pointer<A &>::value:          "
              << std::is_pointer<A &>::value << std::endl;
    std::cout << "std::is_pointer<int>::value:          "
              << std::is_pointer<int>::value << std::endl;
    std::cout << "std::is_pointer<int *>::value:        "
              << std::is_pointer<int *>::value << std::endl;
    std::cout << "std::is_pointer<int **>::value:       "
              << std::is_pointer<int **>::value << std::endl;
    std::cout << "std::is_pointer<int[10]>::value:      "
              << std::is_pointer<int[10]>::value << std::endl;
    std::cout << "std::is_pointer<std::nullptr_t>::value:"
              << std::is_pointer<std::nullptr_t>::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_lvalue_reference<A>::value:   "
              << std::is_lvalue_reference<A>::value << std::endl;
    std::cout << "std::is_lvalue_reference<A&>::value:  "
              << std::is_lvalue_reference<A&>::value << std::endl;
    std::cout << "std::is_lvalue_reference<A&&>::value: "
              << std::is_lvalue_reference < A&& >::value << std::endl;
    std::cout << "std::is_lvalue_reference<int>::value: "
              << std::is_lvalue_reference<int>::value << std::endl;
    std::cout << "std::is_lvalue_reference<int&>::value:    "
              << std::is_lvalue_reference<int&>::value << std::endl;
    std::cout << "std::is_lvalue_reference<int&&>::value:   "
              << std::is_lvalue_reference < int&& >::value << std::endl;
    std::cout << std::endl;

    std::cout << "std::is_rvalue_reference<A>::value:   "
              << std::is_rvalue_reference<A>::value << std::endl;
    std::cout << "std::is_rvalue_reference<A&>::value:  "
              << std::is_rvalue_reference<A&>::value << std::endl;
    std::cout << "std::is_rvalue_reference<A&&>::value: "
              << std::is_rvalue_reference < A&& >::value << std::endl;
    std::cout << "std::is_rvalue_reference<int>::value: "
              << std::is_rvalue_reference<int>::value << std::endl;
    std::cout << "std::is_rvalue_reference<int&>::value:    "
              << std::is_rvalue_reference<int&>::value << std::endl;
    std::cout << "std::is_rvalue_reference<int&&>::value:   "
              << std::is_rvalue_reference < int&& >::value << std::endl;
    std::cout << std::endl;

    return 0;
}
输出
相关推荐
*TQK*16 分钟前
ZZNUOJ(C/C++)基础练习1031——1040(详解版)
c语言·c++·编程知识点
※DX3906※1 小时前
cpp实战项目—string类的模拟实现
开发语言·c++
萌の鱼1 小时前
leetcode 2080. 区间内查询数字的频率
数据结构·c++·算法·leetcode
xianwu5433 小时前
反向代理模块jmh
开发语言·网络·数据库·c++·mysql
敖行客 Allthinker5 小时前
从 UTC 日期时间字符串获取 Unix 时间戳:C 和 C++ 中的挑战与解决方案
linux·运维·服务器·c++
IU宝5 小时前
list的使用,及部分功能的模拟实现(C++)
开发语言·c++
青草地溪水旁6 小时前
c++ list的front和pop_front的概念和使用案例
c++·容器·list
扎量丙不要犟6 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
linhhanpy6 小时前
自制虚拟机(C/C++)(二、分析引导扇区,虚拟机读二进制文件img软盘)
c语言·汇编·c++·python·stm32·操作系统
Stanford_11068 小时前
C++中常用的十大排序方法之4——希尔排序
c++·算法·微信小程序·排序算法·微信公众平台·twitter·微信开放平台