Skip to content

Latest commit

 

History

History
107 lines (86 loc) · 2.56 KB

copyable.md

File metadata and controls

107 lines (86 loc) · 2.56 KB

copyable

  • concepts[meta header]
  • std[meta namespace]
  • concept[meta id-type]
  • cpp20[meta cpp]
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>;
}
  • assignable_from[link /reference/concepts/assignable_from.md]
  • copy_constructible[link /reference/concepts/copy_constructible.md]
  • movable[link /reference/concepts/movable.md]

概要

copyableは、任意の型Tmovableコンセプトを満たし、それに加えてコピー構築・代入が可能であることを表すコンセプトである。

#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");
}
  • std::copyable[color ff0000]

出力

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

処理系

関連項目

参照