Namespaces
Variants
Views
Actions

std::piecewise_construct, std::piecewise_construct_t

From cppreference.com
< cpp‎ | utility
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(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++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
Defined in header <utility>
struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
(1) (since C++11)
(2)
constexpr std::piecewise_construct_t piecewise_construct{};
(since C++11)
(until C++17)
inline constexpr std::piecewise_construct_t piecewise_construct{};
(since C++17)
1) std::piecewise_construct_t is an empty class tag type used to disambiguate between different functions that take two tuple arguments.
2) The constant std::piecewise_construct is an instance of (1).

The overloads that do not use std::piecewise_construct_t assume that each tuple argument becomes the element of a pair. The overloads that use std::piecewise_construct_t assume that each tuple argument is used to construct, piecewise, a new object of specified type, which will become the element of the pair.

[edit] Example

#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);
}

Output:

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

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2510 C++11 the default constructor was non-explicit, which could lead to ambiguity made explicit

[edit] See also

constructs new pair
(public member function of std::pair<T1,T2>)