GeezerVison is showing cpprefjp.github.io — std::polymorphic::コンストラクタ - cpprefjp C++日本語リファレンス
最終更新日時:
2026年08月12日 13時26分34秒 (JST)
Akira Takahashi
が更新
履歴
編集
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から、その型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であること。
適格要件
効果
所有するオブジェクトを、指定された型・引数とアロケータallocを用いて構築する。allocator_arg_t版はallocをaで初期化する。
例外
例
#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 );
}
出力
バージョン
言語
処理系
関連項目
参照