Namespaces
Variants

std:: semiregular

From cppreference.net
헤더에 정의됨 <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]

참고 항목

(C++20)
타입이 regular임을 지정하며, 이는 semiregular 이면서 동시에 equality_comparable 임을 의미합니다
(concept)