Namespaces
Variants

std:: regular

From cppreference.net
定义于头文件 <concepts>
template < class T >
concept regular = std:: semiregular < T > && std:: equality_comparable < T > ;
(C++20 起)

regular 概念规定一个类型是 正则的 ,即该类型可复制、可默认构造且支持相等比较。其满足条件与内置类型(如 int )行为相似,且可通过 == 进行比较的类型。

示例

#include <concepts>
#include <iostream>
template<std::regular T>
struct Single
{
    T value;
    friend bool operator==(const Single&, const Single&) = default;
};
int main()
{
    Single<int> myInt1{4};
    Single<int> myInt2;
    myInt2 = myInt1;
    if (myInt1 == myInt2)
        std::cout << "Equal\n";
    std::cout << myInt1.value << ' ' << myInt2.value << '\n';
}

输出:

Equal
4 4

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 18.6 对象概念 [concepts.object]
  • C++20 标准 (ISO/IEC 14882:2020):
  • 18.6 对象概念 [concepts.object]

参见

规定该类型的对象可被复制、移动、交换及默认构造
(概念)