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

C++标准模板(STL)- 类型支持 (类型属性,检查类型是否拥有强结构相等性,std::has_strong_structural_equality)

类型特性


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

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

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

类型属性

检查类型是否拥有强结构相等性

std::has_strong_structural_equality

template< class T >
struct has_strong_structural_equality;

(C++20 起)

T 拥有强结构相等性,则提供等于 true 的成员常量 value 。对于任何其他类型 value 为 false 。

模板形参

T-要检查的类型

辅助变量模板

template< class T >
inline constexpr bool has_strong_structural_equality_v = has_strong_structural_equality<T>::value;

(C++20 起)

继承自 std::integral_constant

成员常量

value

[静态]

T 拥有强结构相等性则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool

转换对象为 bool ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typebool
typestd::integral_constant<bool, value>

注解

此类型特征可用于检查一个类型能否用作非类型模板形参。

调用示例

#include <type_traits>
#include <cstring>
#include <algorithm>
#include <iostream>namespace std
{
template<typename... Ts> struct make_void
{typedef void type;
};
template<typename... Ts> using void_t = typename make_void<Ts...>::type;template <typename T, typename = void>
struct has_strong_structural_equality : std::false_type {};template <typename T>
struct has_strong_structural_equality<T, std::void_t<decltype(std::declval<T>() == std::declval<T>())>> : std::true_type
{template<typename, typename>static std::false_type test(...);using type = decltype(test<T, T>(0));
};template<typename T, typename U>
using has_strong_structural_equality_t = typename has_strong_structural_equality<T, U>::type;template <typename T>
const bool has_strong_structural_equality_v = has_strong_structural_equality<T>::value;
}class E
{
public:template<class T> E(T&&) { }
};class A {};
class B : public A {};
class C {};
class D
{
public:operator C(){return c;}  C c;
};struct MyStruct
{int x;double y;
};// 自定义比较运算符
bool operator==(const MyStruct& lhs, const MyStruct& rhs)
{return lhs.x == rhs.x && lhs.y == rhs.y;
}int main()
{std::cout << std::boolalpha;std::cout << "std::has_strong_structural_equality<A>::value:       "<< std::has_strong_structural_equality<A>::value << std::endl;std::cout << "std::has_strong_structural_equality<E>::value:       "<< std::has_strong_structural_equality<E>::value << std::endl;std::cout << "std::has_strong_structural_equality<float>::value:   "<< std::has_strong_structural_equality<float>::value << std::endl;std::cout << "std::has_strong_structural_equality<int>::value:     "<< std::has_strong_structural_equality<int>::value << std::endl;std::cout << "std::has_strong_structural_equality<char>::value:    "<< std::has_strong_structural_equality<char>::value << std::endl;std::cout << "std::has_strong_structural_equality<bool>::value:    "<< std::has_strong_structural_equality<bool>::value << std::endl;std::cout << "std::has_strong_structural_equality<MyStruct>::value:"<< std::has_strong_structural_equality<MyStruct>::value << std::endl;std::cout << "-----------------------------------------------" << std::endl;std::cout << std::endl;return 0;
}

输出

std::has_strong_structural_equality<A>::value:       false
std::has_strong_structural_equality<E>::value:       false
std::has_strong_structural_equality<float>::value:   true
std::has_strong_structural_equality<int>::value:     true
std::has_strong_structural_equality<char>::value:    true
std::has_strong_structural_equality<bool>::value:    true
std::has_strong_structural_equality<MyStruct>::value:true
-----------------------------------------------

http://www.lryc.cn/news/413844.html

相关文章:

  • 《中国数据库前世今生》观影——2000年代/数据库分型及国产数据库开端
  • 图的同态Graph Homomorphism与同构Graph Isomorphism
  • 使用 Python 对雷达卫星 sar 图像进行降噪的三种方法
  • C# Unity 面向对象补全计划 之 初识继承方法与多态
  • 突破PyCharm索引瓶颈:提升文件索引速度的策略
  • 体素相关的快速计算
  • Python 爬虫项目实战(二):爬取微博热搜榜
  • 文件解析漏洞复现
  • git push报错 pre-receive hook declined
  • 打造个性化代码审查工具:在Perl中实现自定义审查的艺术
  • RabbitMq架构原理剖析及应用
  • c# 对接第三方接口实现签名
  • 数学建模评价类模型—层次分析法(无数据情况下)
  • 模拟实现strcat(字符串追加)
  • HTTP简单概述
  • 掌握PyCharm代码片段管理器:提升编码效率的秘诀
  • MyBatis动态代理和映射器
  • ShardingSphere中的ShardingJDBC常见分片算法的实现
  • SpringBoot整合Flink CDC实时同步postgresql变更数据,基于WAL日志
  • ThinkPHP事件的使用
  • 【Nuxt】服务端渲染 SSR
  • Spring Boot整合WebSocket
  • 《LeetCode热题100》---<5.③普通数组篇五道>
  • Cocos Creator文档学习记录
  • 插入数据优化 ---大批量数据插入建议使用load
  • 【Linux】一篇总结!什么是重定向?输出重定向的作用是什么?什么又是追加重定向?
  • svn软件总成全内容
  • [激光原理与应用-118]:电源系统的接地详解:小信号的噪声干扰优化,从良好外壳接地开始
  • 回测本身就是一种过度拟合?
  • 什么是Arduino?