当前位置: 首页 > article >正文

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_typebool
typestd::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;
}
输出


http://www.kler.cn/news/109198.html

相关文章:

  • pytest-yaml 测试平台-3.创建执行任务定时执行用例
  • RabbitMQ学习05
  • 网络滤波器/网络滤波器/脉冲变压器要怎样进行测试,一般要测试哪些参数?
  • 云计算概述笔记
  • 建筑能源管理(7)——建筑节能诊断内容
  • RabbitMQ基础
  • 华为OD机考算法题:寻找最大价值的矿堆
  • [毕设记录]@开题调研:一些产品
  • 分类预测 | Matlab实现KOA-CNN-LSTM-selfAttention多特征分类预测(自注意力机制)
  • [动态规划] (一) LeetCode 1137.第N个泰波那契数
  • 刀具磨损状态识别(Python代码,MSCNN_LSTM_Attention模型,初期磨损、正常磨损和急剧磨损分类,解压缩直接运行)
  • An Early Evaluation of GPT-4V(ision)
  • GORM GEN 生成代码如何自定义方法和表名
  • 学习gorm:彻底弄懂Find、Take、First和Last函数的区别
  • 02【Git分支的使用、Git回退、还原】
  • rust重载比较运算符
  • 前端 :用HTML , CSS ,JS 做一个秒表
  • CN考研真题知识点二轮归纳(1)
  • 【Unity PlasticSCM】记录:从介绍 下载 到拉取项目
  • 让谷歌插件单独一个窗口运行
  • TSINGSEE青犀基于AI视频识别技术的平安校园安防视频监控方案
  • 无法查看 spring-boot-starter-parent的pom.xml
  • Linux命令(108)之dirname
  • Mybatis 动态SQL
  • Python mysql 封装备用
  • Go学习第十六章——Gin文件上传与下载
  • Vue路由
  • 基于单片机的温湿度和二氧化碳检测系统设计
  • TensorFlow图像多标签分类实例
  • 【鸿蒙软件开发】ArkTS基础组件之TextTimer(文本显示计时)、TimePicker(时间选择)