namespace std {
template<class T>
concept movable = is_object_v<T> &&
move_constructible<T> &&
assignable_from<T&, T> &&
swappable<T>;
}
概要
movable
は、任意の型T
がオブジェクト型かつムーブ構築・代入が可能であり、必然的にswap
可能であることを表すコンセプトである。
例
#include <iostream>
#include <concepts>
template<std::movable T>
void f(const char* name) {
std::cout << name << " is movable" << std::endl;
}
template<typename T>
void f(const char* name) {
std::cout << name << " is not movable" << std::endl;
}
struct movable {
movable(movable&&) = default;
movable& operator=(movable&&) = default;
};
struct not_movable1 {
not_movable1(not_movable1&&) = delete;
};
struct not_movable2 {
not_movable2& operator=(not_movable2&&) = delete;
};
int main() {
f<int>("int");
f<double>("double");
f<std::nullptr_t>("std::nullptr_t");
f<std::size_t>("std::size_t");
f<movable>("movable");
std::cout << "\n";
f<void>("void");
f<not_movable1>("not_movable1");
f<not_movable2>("not_movable2");
}
出力
int is movable
double is movable
std::nullptr_t is movable
std::size_t is movable
movable is movable
void is not movable
not_movable1 is not movable
not_movable2 is not movable
バージョン
言語
- C++20
処理系
- Clang: ??
- GCC: 10.1
- Visual C++: 2019 Update 3