std::semiregular
來自 cppreference.com
< cpp | 概念 (concepts)
定義於標頭檔案 <concepts> |
||
template< class T > concept semiregular = std::copyable<T> && std::default_initializable<T>; |
(C++20 起) | |
semiregular
概念指定一個型別既是可複製的 (copyable) 又是可預設構造的 (default constructible)。它由行為類似於內建型別(如 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 (概念) |