Spec-Zone.ru › C++

std::semiregular

Определён в заголовочном файле <concepts>
template< class T >
concept semiregular = std::copyable<T> && std::default_initializable<T>;
(начиная с C++20)

Концепция semiregular определяет, что тип является копируемым и конструируемым по умолчанию. Ей удовлетворяют типы, поведение которых аналогично поведению встроенных типов, таких как int, за исключением того, что им не обязательно поддерживать сравнение с ==.

Пример

#include <concepts>
#include <iostream>

template<std::semiregular T>
// Credit Alexander Stepanov
// concepts are requirements on T
// Requirement on T: T is semiregular
// T a(b); or T a = b; => copy constructor
// T a; => default constructor
// a = b; => assignment
struct Single
{
    T value;
    // Aggregation initialization for Single behaves like following constructor:
    // explicit Single(const T& x) : value(x) {}

    // Implicitly declared special member functions behave like following definitions,
    // except that they may have additional properties:
    // Single(const Single& x) : value(x.value) {}
    // Single() {}
    // ~Single() {}
    // Single& operator=(const Single& x) { value = x.value; return *this; }
    // comparison operator is not defined; it is not required by `semiregular` concept
    // bool operator==(Single const& other) const = delete;
};

void print(std::semiregular auto x)
{
    std::cout << x.value << '\n';
}

int main()
{
    Single<int> myInt1{4};      // aggregate initialization: myInt1.value = 4
    Single<int> myInt2(myInt1); // copy constructor
    Single<int> myInt3;         // default constructor
    myInt3 = myInt2;            // copy assignment operator
//  myInt1 == myInt2;           // Error: operator== is not defined

    print(myInt1); // ok: Single<int> is a `semiregular` type
    print(myInt2);
    print(myInt3);

}   // Single<int> variables are destroyed here

Вывод:

4
4
4

Ссылки

  • Стандарт C++23 (ISO/IEC 14882:2024):
    • 18.6 Концепции объектов [concepts.object]
  • Стандарт C++20 (ISO/IEC 14882:2020):
    • 18.6 Концепции объектов [concepts.object]

См. также

regular
(C++20)
определяет, что тип является регулярным, то есть одновременно удовлетворяет концепциям semiregular и equality_comparable
(концепция)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/concepts/semiregular

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API