std::piecewise_construct

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
std::pair
Member functions
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
(C++11)
Deduction guides(C++17)
Helper classes
(C++11)
 
Defined in header <utility>
constexpr std::piecewise_construct_t piecewise_construct{};
(since C++11)
(until C++17)
inline constexpr std::piecewise_construct_t piecewise_construct{};
(since C++17)

The constant std::piecewise_construct is an instance of an empty struct tag type std::piecewise_construct_t.

Example

#include <iostream>
#include <utility>
#include <tuple>
 
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::pair<Foo, Foo> p1(t, t);
    std::pair<Foo, Foo> p2(std::piecewise_construct, t, t);
}

Output:

Constructed a Foo from a tuple
Constructed a Foo from a tuple
Constructed a Foo from an int and a float
Constructed a Foo from an int and a float

See also

tag type used to select correct function overload for piecewise construction
(class)
constructs new pair
(public member function of std::pair<T1,T2>)