名前空間
変種
操作

std::semiregular

From cppreference.com
< cpp‎ | concepts
 
 
 
ヘッダ <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 standard (ISO/IEC 14882:2020)
  • 18.6 オブジェクトコンセプツ [concepts.object]

[編集] 関連項目

(C++20)
型がレギュラーであることを指定します。つまり、semiregular かつ equality_comparable であるということです。
(コンセプト) [編集]
English 日本語 中文(简体) 中文(繁體)