类型萃取类型检查 Type-Traits LibraryType Checks --- C++20

类型萃取:类型检查 Type-Traits Library:Type Checks — C++20

Type-Traits libraryC++11的时候就已经发布,但依然随着C++版本在不断更新

类型检查 Type Checks

每种类型就是十四种主要类型之一

主要类型

template <class T> struct is_void;
template <class T> struct is_integral;
template <class T> struct is_floating_point;
template <class T> struct is_array;
template <class T> struct is_pointer;
template <class T> struct is_null_pointer;
template <class T> struct is_member_object_pointer;
template <class T> struct is_member_function_pointer;
template <class T> struct is_enum;
template <class T> struct is_union;
template <class T> struct is_class;
template <class T> struct is_function;
template <class T> struct is_lvalue_reference;
template <class T> struct is_rvalue_reference;

例子:

#include <iostream>
#include <type_traits>
#include <iostream>
#include <type_traits>struct A
{int a;int f(int) { return 2011; }
};enum E
{e = 1,
};union U
{int u;
};int main()
{using namespace std;cout << boolalpha << '\n'; // boolalpha: bool 以 true 或 false输出cout << is_void<void>::value << '\n'; // true                           cout << is_integral<short>::value << '\n'; // truecout << is_floating_point<double>::value << '\n'; // truecout << is_array<int[]>::value << '\n'; // truecout << is_pointer<int*>::value << '\n'; // truecout << is_null_pointer<nullptr_t>::value << '\n'; // truecout << is_member_object_pointer<int A::*>::value << '\n'; // truecout << is_member_function_pointer<int (A::*)(int)>::value << '\n'; // truecout << is_enum<E>::value << '\n'; // truecout << is_union<U>::value << '\n'; // true cout << is_class<string>::value << '\n'; // truecout << is_function<int*(double)>::value << '\n'; // true	cout << is_lvalue_reference<int&>::value << '\n'; // truecout << is_rvalue_reference<int&&>::value << '\n'; // true
}

让我们试着实验一下这个魔法

#include <iostream>
#include <type_traits>
#include <iostream>
#include <type_traits>namespace fx
{template <class T, T v>struct intergral_constant{static constexpr T value = v;using value_type = T;using type = intergral_constant;constexpr operator value_type() const noexcept{return value;}constexpr value_type operator()() const noexcept{return value;}};using true_type = intergral_constant<bool, true>;using false_type = intergral_constant<bool, false>;template <class T>struct is_integral : public false_type{};template <>struct is_integral<bool> : public true_type{};template <>struct is_integral<char> : public true_type{};template <>struct is_integral<signed char> : public true_type{};template <>struct is_integral<unsigned char> : public true_type{};template <>struct is_integral<wchar_t> : public true_type{};template <>struct is_integral<short> : public true_type{};template <>struct is_integral<int> : public true_type{};template <>struct is_integral<long> : public true_type{};template <>struct is_integral<long long> : public true_type{};template <>struct is_integral<unsigned short> : public true_type{};template <>struct is_integral<unsigned int> : public true_type{};template <>struct is_integral<unsigned long> : public true_type{};template <>struct is_integral<unsigned long long> : public true_type{};
}int main(int argc, char* argv[])
{std::cout << fx::is_integral<int>::value << std::endl;}

fx::is_integral<>::value作为返回值,这是元函数的命名约定

自从C++17之后,有了一个更便捷的方式:

template< class T >
inline constexpr bool is_integral_v = is_integral<T>::value

这样可以使用std::integral_v<T>代替std::integral<int>::value

复合类型 Composite Type Categories

Composite data type - Wikipedia

image-20220525233832046

对于主要类型和复合类型,类型萃取库提供类型属性和类型属性查询

类型属性

template <class T> struct is_const;
template <class T> struct is_volatile;
template <class T> struct is_trivial;
template <class T> struct is_trivially_copyable;
template <class T> struct is_standard_layout;
template <class T> struct is_empty;
template <class T> struct is_polymorphic;
template <class T> struct is_abstract;
template <class T> struct is_final;
template <class T> struct is_aggregate;template <class T> struct is_signed;
template <class T> struct is_unsigned;
template <class T> struct is_bounded_array;
template <class T> struct is_unbounded_array;
template <class T> struct is_scoped_enum;template <class T, class... Args> struct is_constructible;
template <class T> struct is_default_constructible;
template <class T> struct is_copy_constructible;
template <class T> struct is_move_constructible;template <class T, class U> struct is_assignable;
template <class T> struct is_copy_assignable;
template <class T> struct is_move_assignable;template <class T, class U> struct is_swappable_with;
template <class T> struct is_swappable;template <class T> struct is_destructible;template <class T, class... Args> struct is_trivially_constructible;
template <class T> struct is_trivially_default_constructible;
template <class T> struct is_trivially_copy_constructible;
template <class T> struct is_trivially_move_constructible;template <class T, class U> struct is_trivially_assignable;
template <class T> struct is_trivially_copy_assignable;
template <class T> struct is_trivially_move_assignable;
template <class T> struct is_trivially_destructible;template <class T, class... Args> struct is_nothrow_constructible;
template <class T> struct is_nothrow_default_constructible;
template <class T> struct is_nothrow_copy_constructible;
template <class T> struct is_nothrow_move_constructible;template <class T, class U> struct is_nothrow_assignable;
template <class T> struct is_nothrow_copy_assignable;
template <class T> struct is_nothrow_move_assignable;template <class T, class U> struct is_nothrow_swappable_with;
template <class T> struct is_nothrow_swappable;template <class T> struct is_nothrow_destructible;template <class T> struct has_virtual_destructor;template <class T> struct has_unique_object_representations;

类型属性查询

template <class T> struct alignment_of;
template <class T> struct rank;
template <class T, unsigned I = 0> struct extent;

template struct has_virtual_destructor;

template struct has_unique_object_representations;


### 类型属性查询```cpp
template <class T> struct alignment_of;
template <class T> struct rank;
template <class T, unsigned I = 0> struct extent;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407928.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

转:如何在 LoadRunner 脚本中做关联 (Correlation)

如何在 LoadRunner 脚本中做关联 (Correlation) 当录制脚本时&#xff0c;VuGen会拦截client端&#xff08;浏览器&#xff09;与server端&#xff08;网站服务器&#xff09;之间的对话&#xff0c;并且通通记录下来&#xff0c;产生脚本。在VuGen的Recording Log中&#xff0…

开博碎语

结束了5月26号的软考&#xff0c;就萌生了建一个技术博客的想法-----技术或许太空泛&#xff0c;其实就是把工作中&#xff0c;学习上技术方面的一些资料&#xff0c;一些体会汇聚一起&#xff0c;呈现出来&#xff0c;博客当然是个不错的选择。baidu一下&#xff0c;技术博客为…

DateTime时间的比较问题

关于DateTime时间的比较问题&#xff0c;我现在不清楚使用> < 和DateTime.Compare(t1, t2)、t1.CompareTo(t2)的区别。 下面是一个简单的测试程序&#xff0c;至少到现在为止&#xff0c;我还没有发现这两种比较的区别之处&#xff0c;大家有任何自己的想法&#xff0c;…

模板元编程 Template Metaprogramming--- C++ 20

模板元编程(一) Template Metaprogramming— C 20 在编译期进行类型操作 举个例子: std::move在概念上应该这样实现(实际并不是这么做的): static_cast<std::remove_reference<decltype(arg)>::type&&>(arg);意义上,std::move首先获取它的参数arg,推断…

6月,启蒙篇

6月&#xff0c;哈哈 在过去的4月与5月里我一直放荡着自己&#xff0c;我发现一个人没有目标与理想后是这样的无耐 &#xff0c;也是这样的无所做为。在那两个月里我放弃了所有的程序&#xff0c;我只是在计算机前玩游戏 。我原本以为这样会过得开心点&#xff0c;会让自己放松…

JS 计算日期天数差

function dayDiffer(startDate,endDate){console.info((endDate.getTime - startDate.getTime())/(24*60*60*1000));return Math.floor((endDate.getTime() - startDate.getTime())/(24*60*60*1000)); }转载于:https://www.cnblogs.com/jsczljh/p/3647395.html

自定义控件-实现TextBox的禁止粘贴

开发环境&#xff1a;Visual Studio .net 2005 Windows XP sp2 professional 新建->项目&#xff0d;>Windows控件库: 新建一个类&#xff0c;继承自TextBox类&#xff0c;具体源代码如下&#xff1a; using System;using System.Collections.Generic;using System.Comp…

模板元编程(二) Template Metaprogramming ---C++ 20

模板元编程(二) Template Metaprogramming —C 20 现在我们介绍参数与模板参数混合使用 先看一下例子: #include <iostream>int power(int m, int n) {int r 1;for (int k 1; k < n; k) r * m;return r; }template <int m, int n> struct Power {static in…

探索 ASP.NET Futures (Part 2 - Search Enabled)

在本系列的上一篇文章中&#xff0c;我们探索了ASP.NET Futures (May CTP)的SearchSiteMap功能&#xff0c;说明了如何将ASP.NET的SiteMap影射为符合Sitemaps协议的XML以便搜索引擎更好的抓取我们的站点。然而让搜索引擎更好的抓取我们的站点了&#xff0c;这部分的优化却仅仅对…

让窗体获得焦点,一定会有您用到的时候

开发环境&#xff1a;Visual Studio .NET 2005 下的Windows Form Application 应用场景: 当我们有个窗体中的数据发生了变化而此窗体又没有获得焦点(不是用户操作的当前窗口)的时候&#xff0c;我们希望它获得焦点&#xff0c;这样用户就可以立刻发现它上面的数据发生了变化。…

容器和算法的改进 --- C++20

容器和算法的改进 — C20 C 20对容器和算法有很多的改进 std::vector 和std::string支持constexpr所有容器支持consistent container erasure , contains新的算法移动元素 std::shift_left可以检查 std::string 的前缀和后缀 支持 constexpr 的容器和算法 C 20的std::vecto…

CodeSmith基础(二)

本文将介绍CodeSmith与数据库进行交互生成相应的存储过程&#xff0c;本例使用的数据库为SQL Server 2000。 在与数据库进行交互时&#xff0c;我们使用到了一个CodeSmith自带的组件SchemaExplorer&#xff0c;利用这个组件我们可以访问数据库的数据表、存储过程、视图等…

[Android]使用ViewPager实现图片滑动展示

在淘宝等电商的APP首页经常能看到大幅的广告位&#xff0c;通常有多幅经常更新的图片用于展示促销信息&#xff0c;如下图所示&#xff1a; 通常会自动滚动&#xff0c;也可以根据手势滑动。我没有研究过人家的APP是通过什么实现的&#xff0c;可能有第三方已经封装好的控件可以…

react(85)--error:Error creating bean with name ‘onlineStudyController‘:

Error creating bean with name onlineStudyController: Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)" 接口定义了同样的值

c#获取当前应用程序所在路径

一、获取当前文件的路径1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径&#xff0c;包括文件名。2. System.Environment.CurrentDirectory 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。3. System.IO.…

requires表达式 ---C++ 20 模板

requires表达式 —C 20 模板 requires还可以接一个表达式,该表达式也是一个纯右值表达式,表达式为true时满足约束条件,false则不满足约束条件 requires表达式的判定标准:对requires表达式进行模板实参的替换,如果替换之后出现无效类型,或者违反约束条件,则值为false,反之为tr…

读古诗系列--(两首)题都城南庄/江楼感旧

题都城南庄 唐 崔护去年今日此门中&#xff0c; 人面桃花相映红。 人面不知何处去&#xff0c; 桃花依旧笑春风。 江楼感旧 唐 赵嘏 独上江楼思渺然&#xff0c;月光如水水如天。 同来望月人何在&#xff1f;风景依稀似去年。前为课本所学&#xff0c;皆知&#xff…