C++标准模板(STL)- 类型支持 (类型属性,is_bounded_array,is_unbounded_array)

2023-11-06 14:20

本文主要是介绍C++标准模板(STL)- 类型支持 (类型属性,is_bounded_array,is_unbounded_array),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

类型特性

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

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

定义于<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_bounded_array

template< class T >
struct is_bounded_array;

(C++20 起)

检查 T 是否为拥有已知边界的数组类型。若 T 是有已知边界的数组则提供等于 true 的成员常量 value 。否则 value 等于 false 。

模板形参
T-要检查的类型
辅助变量模板

template< class T >
inline constexpr bool is_bounded_array_v = is_bounded_array<T>::value;

(C++20 起)
可能的实现
template<class T>
struct is_bounded_array: std::false_type {};template<class T, std::size_t N>
struct is_bounded_array<T[N]> : std::true_type {};
调用示例
#include <iostream>
#include <type_traits>namespace std
{
template<class T>
struct is_bounded_array: std::false_type {};template<class T, std::size_t N>
struct is_bounded_array<T[N]> : std::true_type {};
}class A {};int main()
{std::cout << std::boolalpha;std::cout << "std::is_bounded_array<std::string>::value:    "<< std::is_bounded_array<std::string>::value << std::endl;std::cout << "std::is_bounded_array<A>::value:              "<< std::is_bounded_array<A>::value << std::endl;std::cout << "std::is_bounded_array<A[]>::value:            "<< std::is_bounded_array<A[]>::value << std::endl;std::cout << "std::is_bounded_array<A[3]>::value:           "<< std::is_bounded_array<A[3]>::value << std::endl;std::cout << "std::is_bounded_array<float>::value:          "<< std::is_bounded_array<float>::value << std::endl;std::cout << "std::is_bounded_array<int>::value:            "<< std::is_bounded_array<int>::value << std::endl;std::cout << "std::is_bounded_array<int[]>::value:          "<< std::is_bounded_array<int[]>::value << std::endl;std::cout << "std::is_bounded_array<int[3]>::value:         "<< std::is_bounded_array<int[3]>::value << std::endl;return 0;
}
输出

检查类型是否为有未知边界的数组类型

std::is_unbounded_array

template< class T >
struct is_unbounded_array;

(C++20 起)

检查 T 是否为未知边界数组类型。若 T 是有未知边界的数组类型则提供等于 true, 的成员常量 value 。否则 value 等于 false 。

模板形参
T-要检查的类型
辅助变量模板

template< class T >
inline constexpr bool is_unbounded_array_v = is_unbounded_array<T>::value;

(C++20 起)
可能的实现
template<class T>
struct is_unbounded_array: std::false_type {};template<class T>
struct is_unbounded_array<T[]> : std::true_type {};
调用示例
#include <iostream>
#include <type_traits>namespace std
{
template<class T>
struct is_unbounded_array: std::false_type {};template<class T>
struct is_unbounded_array<T[]> : std::true_type {};
}class A {};int main()
{std::cout << std::boolalpha;std::cout << "std::is_unbounded_array<std::string>::value:    "<< std::is_unbounded_array<std::string>::value << std::endl;std::cout << "std::is_unbounded_array<A>::value:              "<< std::is_unbounded_array<A>::value << std::endl;std::cout << "std::is_unbounded_array<A[]>::value:            "<< std::is_unbounded_array<A[]>::value << std::endl;std::cout << "std::is_unbounded_array<A[3]>::value:           "<< std::is_unbounded_array<A[3]>::value << std::endl;std::cout << "std::is_unbounded_array<float>::value:          "<< std::is_unbounded_array<float>::value << std::endl;std::cout << "std::is_unbounded_array<int>::value:            "<< std::is_unbounded_array<int>::value << std::endl;std::cout << "std::is_unbounded_array<int[]>::value:          "<< std::is_unbounded_array<int[]>::value << std::endl;std::cout << "std::is_unbounded_array<int[3]>::value:         "<< std::is_unbounded_array<int[3]>::value << std::endl;return 0;
}
输出

这篇关于C++标准模板(STL)- 类型支持 (类型属性,is_bounded_array,is_unbounded_array)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/357127

相关文章

C++迭代器失效的避坑指南

《C++迭代器失效的避坑指南》在C++中,迭代器(iterator)是一种类似指针的对象,用于遍历STL容器(如vector、list、map等),迭代器失效是指在对容器进行某些操作后... 目录1. 什么是迭代器失效?2. 哪些操作会导致迭代器失效?2.1 vector 的插入操作(push_back,

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a