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

相关文章

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.