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

履歴 編集

function
<memory>

std::polymorphic::コンストラクタ(C++26)

explicit constexpr 
  polymorphic();                             // (1)

explicit constexpr
  polymorphic(allocator_arg_t,
              const Allocator& a);           // (2)

constexpr
  polymorphic(const polymorphic& other);     // (3)

constexpr
  polymorphic(allocator_arg_t,
              const Allocator& a,
              const polymorphic& other);     // (4)

constexpr
  polymorphic(polymorphic&& other) noexcept; // (5)

constexpr
  polymorphic(allocator_arg_t,
              const Allocator& a,
              polymorphic&& other) noexcept(see below); // (6)

template <class U = T>
explicit constexpr
  polymorphic(U&& u);                          // (7)

template <class U = T>
explicit constexpr
  polymorphic(allocator_arg_t,
              const Allocator& a,
              U&& u);                          // (8)

template <class U, class... Ts>
explicit constexpr
  polymorphic(in_place_type_t<U>,
              Ts&&... ts);                     // (9)

template <class U, class... Ts>
explicit constexpr
  polymorphic(allocator_arg_t,
              const Allocator& a,
              in_place_type_t<U>, Ts&&... ts); // (10)

template <class U, class I, class... Us>
explicit constexpr
  polymorphic(in_place_type_t<U>,
              initializer_list<I> ilist,
              Us&&... us);                    // (11)

template <class U, class I, class... Us>
explicit constexpr
  polymorphic(allocator_arg_t, const Allocator& a,
              in_place_type_t<U>,
              initializer_list<I> ilist, Us&&... us); // (12)

概要

polymorphicオブジェクトを構築する。allocator_arg_tを第1引数に取るオーバーロードは、使用するアロケータaを明示的に指定する。

  • (1), (2) : デフォルトコンストラクタ。Tデフォルト構築して所有する。
  • (3), (4) : コピーコンストラクタ。otherが保持する派生型のオブジェクトをディープコピーして所有する。otherが無効値状態の場合、構築されるオブジェクトも無効値状態となる。
  • (5), (6) : ムーブコンストラクタ。otherが保持するオブジェクトの所有権を移す。
  • (7), (8) : 単一の引数uから、その型UTから派生していてもよい)のオブジェクトを構築して所有する。
  • (9), (10) : in_place_type_t<U>をマーカーとして、型Uのオブジェクトを引数ts...から直接構築して所有する。
  • (11), (12) : in_place_type_t<U>をマーカーとして、型Uのオブジェクトを初期化子リストと引数us...から直接構築して所有する。

テンプレートパラメータ制約

適格要件

効果

所有するオブジェクトを、指定された型・引数とアロケータallocを用いて構築する。allocator_arg_t版はallocaで初期化する。

例外

noexcept(allocator_traits<Allocator>::is_always_equal::value)

#include <cassert>
#include <memory>

struct Base {
  virtual ~Base() = default;
  virtual int f() const { return 0; }
};
struct Derived : Base {
  int x = 42;
  Derived() = default;
  Derived(int x) : x(x) {}
  int f() const override { return x; }
};

int main()
{
  // (9) in_place_typeで派生型Derivedを直接構築
  std::polymorphic<Base> a{std::in_place_type<Derived>};
  assert(a->f() == 42);

  // (7) 派生型の値から構築
  std::polymorphic<Base> b{Derived{10}};
  assert(b->f() == 10);

  // (3) コピー構築(派生型Derivedのディープコピー)
  std::polymorphic<Base> c = a;
  assert(c->f() == 42);
}

出力

バージョン

言語

  • C++26

処理系

関連項目

参照