namespace std {
template<class T>
concept copyable =
copy_constructible<T> &&
movable<T> &&
assignable_from<T&, T&> &&
assignable_from<T&, const T&> &&
assignable_from<T&, const T>;
}
概要
copyable
は、任意の型T
がmovable
コンセプトを満たし、それに加えてコピー構築・代入が可能であることを表すコンセプトである。
例
#include <iostream>
#include <concepts>
template<std::copyable T>
void f(const char* name) {
std::cout << name << " is copyable" << std::endl;
}
template<typename T>
void f(const char* name) {
std::cout << name << " is not copyable" << std::endl;
}
struct copyable {
copyable(const copyable&) = default;
copyable& operator=(const copyable&) = default;
};
struct movable {
movable(movable&&) = default;
movable& operator=(movable&&) = default;
};
struct not_copyable1 {
not_copyable1(const not_copyable1&) = delete;
};
struct not_copyable2 {
not_copyable2& operator=(const not_copyable2&) = delete;
};
int main() {
f<int>("int");
f<double>("double");
f<std::nullptr_t>("std::nullptr_t");
f<std::size_t>("std::size_t");
f<copyable>("copyable");
std::cout << "\n";
f<void>("void");
f<movable>("movable");
f<not_copyable1>("not_copyable1");
f<not_copyable2>("not_copyable2");
}
46
not_copyable2& operator=(const not_copyable2&) = delete;
#include <iostream>
#include <concepts>
template<std::copyable T>
void f(const char* name) {
std::cout << name << " is copyable" << std::endl;
}
template<typename T>
void f(const char* name) {
std::cout << name << " is not copyable" << std::endl;
}
struct copyable {
copyable(const copyable&) = default;
copyable& operator=(const copyable&) = default;
};
出力
int is copyable
double is copyable
std::nullptr_t is copyable
std::size_t is copyable
copyable is copyable
void is not copyable
movable is not copyable
not_copyable1 is not copyable
not_copyable2 is not copyable
バージョン
言語
- C++20
処理系
- Clang: ??
- GCC: 10.1 ✅
- Visual C++: 2019 Update 3 ✅