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から、その型U(Tから派生していてもよい)のオブジェクトを構築して所有する。 - (9), (10) :
in_place_type_t<U>をマーカーとして、型Uのオブジェクトを引数ts...から直接構築して所有する。 - (11), (12) :
in_place_type_t<U>をマーカーとして、型Uのオブジェクトを初期化子リストと引数us...から直接構築して所有する。
テンプレートパラメータ制約
- (7), (8) :
remove_cvref_t<U>をUUとするとき、UUがpolymorphicでもin_place_type_tの特殊化でもなく、derived_from<UU, T>、is_constructible_v<UU, U>、is_copy_constructible_v<UU>がいずれもtrueであること。(7)はさらにis_default_constructible_v<Allocator>がtrueであること。 - (9), (10) :
is_same_v<remove_cvref_t<U>, U>、derived_from<U, T>、is_constructible_v<U, Ts...>、is_copy_constructible_v<U>がいずれもtrueであること。(9)はさらにis_default_constructible_v<Allocator>がtrueであること。 - (11), (12) :
is_same_v<remove_cvref_t<U>, U>、derived_from<U, T>、is_constructible_v<U, initializer_list<I>&, Us...>、is_copy_constructible_v<U>がいずれもtrueであること。(11)はさらにis_default_constructible_v<Allocator>がtrueであること。
適格要件
- (1), (2) :
is_default_constructible_v<T>とis_copy_constructible_v<T>がともにtrueであること。
効果
所有するオブジェクトを、指定された型・引数とアロケータallocを用いて構築する。allocator_arg_t版はallocをaで初期化する。
例外
- すべてのオーバーロード :
allocator_traits<Allocator>::allocateまたはallocator_traits<Allocator>::constructが例外を送出した場合を除いて、例外を送出しない。 - (6) : 以下と等価な
noexcept指定を持つ:
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
処理系
- Clang: 22 ❌
- GCC: 16.1 ✅
- Visual C++: 2026 Update 2 ❌