namespace std {
template<class T>
concept move_constructible = constructible_from<T, T> && convertible_to<T, T>;
}
概要
move_constructibleは、任意の型Tがムーブ構築可能であること表すコンセプトである。
モデル
Tがオブジェクト型ならばrvをTの右辺値、u2をrvと等値なTのオブジェクトとすると、このrv, u2について以下の条件を満たす場合に限って型Tはmove_constructibleのモデルである。
T u = rv;の定義の後ではuとu2は等値であることT(rv)はu2と等値であることTがconstではないのであれば、上記の2つの条件内の式の後のrvは有効だが未規定な状態となる。そうでなければrvは変更されない。- 標準ライブラリの型のオブジェクトはムーブされた後では有効だが未規定な状態となる。
例
#include <iostream>
#include <concepts>
template<std::move_constructible T>
void f(const char* name) {
std::cout << name << " is move constructible" << std::endl;
}
template<typename T>
void f(const char* name) {
std::cout << name << " is not move constructible" << std::endl;
}
struct S {
S(S&&) = delete;
S(int m) : n(m) {}
int n = 0;
};
struct M {
M(M&&) = default;
};
int main() {
f<int>("int");
f<S>("S");
f<M>("M");
}
出力
int is move constructible
S is not move constructible
M is move constructible
バージョン
言語
- C++20
処理系
- Clang: ??
- GCC: 10.1 ✅
- Visual C++: 2019 Update 3 ✅