std::piecewise_construct, std::piecewise_construct_t
ja.cppreference.net より
| ヘッダ <utility>
|
||
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
|
で定義 (1) | (C++11以上) |
constexpr std::piecewise_construct_t piecewise_construct{};
|
(2) | (C++11以上) (C++17以上でインライン) |
1)
std::piecewise_construct_t は、2つのtuple引数を取る異なる関数を区別するために使用される空のクラスタグ型です。2) 定数
std::piecewise_construct は (1) のインスタンスです。std::piecewise_construct_t を使用しないオーバーロードは、各tuple引数がpairの要素になると想定します。std::piecewise_construct_t を使用するオーバーロードは、各tuple引数を使用して、指定された型の新しいオブジェクトをピースごとに構築し、それがpairの要素になると想定します。
標準ライブラリ
以下の標準ライブラリの型と関数は、曖昧性除去タグとしてこれを使用します:
|
二値タプル、すなわち値のペアを実装する
(クラステンプレート) |
|
|
(C++20)
|
指定された型が必要とするuses-allocator構築の種類に一致する引数リストを準備する
(関数テンプレート) |
同じ値を繰り返し生成することによって生成されるシーケンスからなる
view
(クラステンプレート) (カスタマイゼーションポイントオブジェクト) |
例
このコードを実行
#include <iostream> #include <tuple> #include <utility> struct Foo { Foo(std::tuple<int, float>) { std::cout << "Constructed a Foo from a tuple\n"; } Foo(int, float) { std::cout << "Constructed a Foo from an int and a float\n"; } }; int main() { std::tuple<int, float> t(1, 3.14); std::cout << "Creating p1...\n"; std::pair<Foo, Foo> p1(t, t); std::cout << "Creating p2...\n"; std::pair<Foo, Foo> p2(std::piecewise_construct, t, t); }
出力:
Creating p1... Constructed a Foo from a tuple Constructed a Foo from a tuple Creating p2... Constructed a Foo from an int and a float Constructed a Foo from an int and a float
欠陥報告
以下の動作変更に関する欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2510 | C++11 | デフォルトコンストラクタが非explicitであり、曖昧性を引き起こす可能性があった | explicitに変更 |
関連項目
|
新しいpairを構築する
(
std::pair<T1,T2>
のpublicメンバー関数)
|