std::tuple<Types...>::tuple
: cppreference.com
<tbody>
</tbody>
constexpr tuple(); |
(1) | (C++11) (explicit) |
tuple( const Types&... args ); |
(2) | (C++11) (C++14constexpr) (explicit) |
template< class... UTypes > tuple( UTypes&&... args ); |
(3) | (C++11) (C++14constexpr) (explicit) |
template< class... UTypes > tuple( const tuple<UTypes...>& other ); |
(4) | (C++11) (C++14constexpr) (explicit) |
template <class... UTypes> tuple( tuple<UTypes...>&& other ); |
(5) | (C++11) (C++14constexpr) (explicit) |
template< class U1, class U2 > tuple( const pair<U1,U2>& p ); |
(6) | (C++11) (C++14constexpr) (explicit) |
template< class U1, class U2 > tuple( pair<U1,U2>&& p ); |
(7) | (C++11) (C++14constexpr) (explicit) |
tuple( const tuple& other ) = default; |
(8) | (C++11) |
tuple( tuple&& other ) = default; |
(9) | (C++11) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a ); |
(10) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const Types&... args ); |
(11) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, UTypes&&... args ); |
(12) | (C++11) (C++20constexpr) (explicit) |
template <class Alloc, class... UTypes> tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>& other ); |
(13) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>&& other ); |
(14) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, const pair<U1, U2>& p ); |
(15) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, pair<U1, U2>&& p ); |
(16) | (C++11) (C++20constexpr) (explicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const tuple& other ); |
(17) | (C++11) (C++20constexpr) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, tuple&& other ); |
(18) | (C++11) (C++20constexpr) |
i std::is_default_constructible<Ti>::value true i Ti {} explicit 2)
sizeof...(Types) >= 1 i std::is_copy_constructible<Ti>::value true i std::is_convertible<const Ti&, Ti>::value false explicit 3)
std::forward<UTypes>(args) sizeof...(Types) == sizeof...(UTypes) sizeof...(Types) >= 1 i std::is_constructible<Ti, Ui&&>::value true i std::is_convertible<Ui&&, Ti>::value false explicit 4)
sizeof...(UTypes) i i std::get<i>(other) sizeof...(Types) == sizeof...(UTypes) i std::is_constructible_v<Ti, const Ui&> true sizeof...(Types) != 1 (Types... T UTypes... U ) std::is_convertible_v<const tuple<U>&, T>, std::is_constructible_v<T, const tuple<U>&>, std::is_same_v<T, U> false i std::is_convertible<const Ui&, Ti>::value false explicit 5)
sizeof...(UTypes) i i std::forward<Ui>(std::get<i>(other)) sizeof...(Types) == sizeof...(UTypes) i std::is_constructible_v<Ti, Ui&&> true sizeof...(Types) != 1 (Types... T UTypes... U ) std::is_convertible_v<tuple<U>, T>, std::is_constructible_v<T, tuple<U>>, std::is_same_v<T, U> false i std::is_convertible<Ui&&, Ti>::value false explicit 6)
p.first 2 p.second 2sizeof...(Types) == 2 std::is_constructible<T0,const U1&>::value std::is_constructible<T1, const U2&>::value true std::is_convertible<const U1&, T0>::value std::is_convertible<const U2&, T1>::value false explicit 7)
std::forward<U1>(p.first) 2 std::forward<U2>(p.second) 2sizeof...(Types) == 2 std::is_constructible<T0, U1&&>::value std::is_constructible<T1, U2&&>::value true std::is_convertible<U1&&, T0>::value std::convertible<U2&&, T1>::value false explicit 8)
other constexpr constexpr std::tuple<> constexpr i std::is_copy_constructible<Ti>::value true 9) i
std::forward<Ui>(std::get<i>(other)) constexpr constexpr std::tuple<> constexpr I std::is_move_constructible<Ti>::value true | args | - | |
| other | - | |
| p | - | 2 |
| a | - |
explicit
std::tuple<int, int> foo_tuple()
{
return {1, -1}; // Error before N4387
return std::make_tuple(1, -1); // Always works
}
explicit
using namespace std::chrono;
void launch_rocket_at(std::tuple<hours, minutes, seconds>);
launch_rocket_at({hours(1), minutes(2), seconds(3)}); // OK
launch_rocket_at({1, 2, 3}); // Error: int is not implicitly convertible to duration
launch_rocket_at(std::tuple<hours, minutes, seconds>{1, 2, 3}); // OK
C++
| DR | |||
|---|---|---|---|
| N4387 | C++11 | some constructors were explicit, preventing useful behavior | most constructors made conditionally-explicit |
| LWG 2510 | C++11 | default constructor was implicit | made conditionally-explicit |
| LWG 3158 | C++11 | the uses-allocator constructor corresponding to default constructor was implicit |
made conditionally-explicit |
Run this code
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <memory>
// helper function to print a tuple of any size
template<class Tuple, std::size_t N>
struct TuplePrinter {
static void print(const Tuple& t)
{
TuplePrinter<Tuple, N-1>::print(t);
std::cout << ", " << std::get<N-1>(t);
}
};
template<class Tuple>
struct TuplePrinter<Tuple, 1>{
static void print(const Tuple& t)
{
std::cout << std::get<0>(t);
}
};
template<class... Args>
void print(const std::tuple<Args...>& t)
{
std::cout << "(";
TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
std::cout << ")\n";
}
// end helper function
int main()
{
std::tuple<int, std::string, double> t1;
std::cout << "Value-initialized: "; print(t1);
std::tuple<int, std::string, double> t2(42, "Test", -3.14);
std::cout << "Initialized with values: "; print(t2);
std::tuple<char, std::string, int> t3(t2);
std::cout << "Implicitly converted: "; print(t3);
std::tuple<int, double> t4(std::make_pair(42, 3.14));
std::cout << "Constructed from a pair"; print(t4);
// given Allocator my_alloc with a single-argument constructor my_alloc(int)
// use my_alloc(1) to allocate 10 ints in a vector
std::vector<int, my_alloc> v(10, 1, my_alloc(1));
// use my_alloc(2) to allocate 10 ints in a vector in a tuple
std::tuple<int, std::vector<int, my_alloc>, double> t5(std::allocator_arg,
my_alloc(2), 42, v, -3.14);
}
:
Value-initialized: (0, , 0)
Initialized with values: (42, Test, -3.14)
Implicitly converted: (*, Test, -3)
Constructed from a pair(42, 3.14)
tuple () | |
tuple () | |
tuple () |