• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    concept
    <concepts>

    std::copy_constructible

    namespace std {
      template<class T>
      concept copy_constructible =
        move_constructible<T> &&
        constructible_from<T, T&> && convertible_to<T&, T> &&
        constructible_from<T, const T&> && convertible_to<const T&, T> &&
        constructible_from<T, const T> && convertible_to<const T, T>;
    }
    

    概要

    copy_constructibleは、任意の型Tがコピー構築可能であること表すコンセプトである。

    モデル

    Tオブジェクト型ならばvTの左辺値(const付きも含む)もしくはconst Tの右辺値とすると、このvについて以下の条件を満たす場合に限って型Tcopy_constructibleのモデルである。

    • T u = v;の定義の後ではuvは等値であること
    • T(v)uと等値であること

    #include <iostream>
    #include <concepts>
    
    template<std::copy_constructible T>
    void f(const char* name) {
      std::cout << name << " is copy constructible" << std::endl;
    }
    
    template<typename T>
    void f(const char* name) {
      std::cout << name << " is not copy constructible" << std::endl;
    }
    
    struct S {
      S(const S&) = delete;
    
      S(int m) : n(m) {}
    
      int n = 0;
    };
    
    struct M {
      M(M&&) = delete;
    };
    
    struct C {
      C(const C&) = default;
    };
    
    int main() {
      f<int>("int");
      f<S>("S");
      f<M>("M");
      f<C>("C");
    }   
    

    出力

    int is copy constructible
    S is not copy constructible
    M is not copy constructible
    C is copy constructible
    

    バージョン

    言語

    • C++20

    処理系

    関連項目

    参照