Namespaces
Variants

std:: piecewise_construct, std:: piecewise_construct_t

From cppreference.net
Utilities library
ヘッダー <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つのタプル引数を取る異なる関数を区別するために使用される空のクラスタグ型です。
2) 定数 std::piecewise_construct (1) のインスタンスです。

std::piecewise_construct_t を使用しないオーバーロードは、各タプル引数がペアの要素になると仮定します。 std::piecewise_construct_t を使用するオーバーロードは、各タプル引数が指定された型の新しいオブジェクトをピース単位で構築するために使用され、それがペアの要素になると仮定します。

目次

標準ライブラリ

以下の標準ライブラリの型と関数は、曖昧性除去タグとしてこれを使用します:

二値タプル、すなわち値のペアを実装する
(クラステンプレート)
指定された型が必要とする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メンバー関数)