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_type | bool |
| type | std::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
-----------------------------------------------
相关推荐
apocelipes17 小时前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
郝学胜_神的一滴2 天前
CMake 034:生成器表达式:解耦构建时序、精简分支逻辑的终极利器
c++·cmake
见过夏天3 天前
C++ 基础入门完全指南
c++
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
BadBadBad__AK5 天前
线段树维护区间 k 次方和
c++·数学·算法·stl
卷无止境5 天前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境5 天前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴6 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境8 天前
C++ 的Eigen 库全解析
c++
卷无止境8 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端