在线文档教程
C++
应用 | Utilities

std::tuple::tuple

std::tuple::tuple

Defined in header
(1)
constexpr tuple((since C++11) (until C++17)
/*EXPLICIT*/ constexpr tuple((since C++17)
(2)
explicit tuple( const Types&... args (since C++11) (until C++14)
explicit constexpr tuple( const Types&... args (since C++14) (until C++17)
/*EXPLICIT*/ constexpr tuple( const Types&... args (since C++17)
(3)
template< class... UTypes > explicit tuple( UTypes&&... args (since C++11) (until C++14)
template< class... UTypes > explicit constexpr tuple( UTypes&&... args (since C++14) (until C++17)
template< class... UTypes > /*EXPLICIT*/ constexpr tuple( UTypes&&... args (since C++17)
(4)
template< class... UTypes > tuple( const tuple<UTypes...>& other (since C++11) (until C++14)
template< class... UTypes > constexpr tuple( const tuple<UTypes...>& other (since C++14) (until C++17)
template< class... UTypes > /*EXPLICIT*/ constexpr tuple( const tuple<UTypes...>& other (since C++17)
(5)
template <class... UTypes> tuple( tuple<UTypes...>&& other (since C++11) (until C++14)
template <class... UTypes> constexpr tuple( tuple<UTypes...>&& other (since C++14) (until C++17)
template <class... UTypes> /*EXPLICIT*/ constexpr tuple( tuple<UTypes...>&& other (since C++17)
(6)
template< class U1, class U2 > tuple( const pair<U1,U2>& p (since C++11) (until C++14)
template< class U1, class U2 > constexpr tuple( const pair<U1,U2>& p (since C++14) (until C++17)
template< class U1, class U2 > /*EXPLICIT*/ constexpr tuple( const pair<U1,U2>& p (since C++17)
(7)
template< class U1, class U2 > tuple( pair<U1,U2>&& p (since C++11) (until C++14)
template< class U1, class U2 > constexpr tuple( pair<U1,U2>&& p (since C++14) (until C++17)
template< class U1, class U2 > /*EXPLICIT*/ constexpr tuple( pair<U1,U2>&& p (since C++17)
tuple( const tuple& other ) = default;(8)(since C++11)
tuple( tuple&& other ) = default;(9)(since C++11)
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a (10)(since C++11)
(11)
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const Types&... args (since C++11) (until C++17)
template< class Alloc > /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, const Types&... args (since C++17)
(12)
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, UTypes&&... args (since C++11) (until C++17)
template< class Alloc, class... UTypes > /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, UTypes&&... args (since C++17)
(13)
template <class Alloc, class... UTypes> tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>& other (since C++11) (until C++17)
template <class Alloc, class... UTypes> /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>& other (since C++17)
(14)
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>&& other (since C++11) (until C++17)
template< class Alloc, class... UTypes > /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>&& other (since C++17)
(15)
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, const pair<U1, U2>& p (since C++11) (until C++17)
template< class Alloc, class U1, class U2 > /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, const pair<U1, U2>& p (since C++17)
(16)
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, pair<U1, U2>&& p (since C++11) (until C++17)
template< class Alloc, class U1, class U2 > /*EXPLICIT*/ tuple( std::allocator_arg_t, const Alloc& a, pair<U1, U2>&& p (since C++17)
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const tuple& other (17)(since C++11)
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, tuple&& other (18)(since C++11)

构造一个新的元组。

1%29默认构造函数。值初始化所有元素。

Requires that std::is_default_constructible::value is true for all i(until C++17)
This overload only participates in overload resolution if std::is_default_constructible<Ti>::value is true for all i The constructor is explicit if and only if Ti is not implicitly default-constructible for at least one i.(since C++17)

2%29直接构造函数。使用相应的参数初始化元组的每个元素。

此重载只参与在下列情况下的重载解决方案:sizeof...(Types) >= 1和std::is_copy_constructible<Ti>::value是true为所有人i...

This constructor is explicit if and only if std::is_convertible::value is false for at least one i.(since C++17)

3%29转换构造函数。中的相应值初始化元组的每个元素。std::forward<Utypes>(args)...

Requires that sizeof...(Types) == sizeof...(UTypes) This overload only participates in overload resolution if sizeof...(Types) >= 1 and std::is_constructible::value is true for all i.(until C++17)
This overload only participates in overload resolution if sizeof...(Types) == sizeof...(UTypes) and sizeof...(Types) >= 1 and std::is_constructible<Ti, Ui&&>::value is true for all i. The constructor is explicit if and only if std::is_convertible<Ui&&, Ti>::value is false for at least one i.(since C++17)

4%29转换复制构造函数.。为所有人i在sizeof...(UTypes),将元组的ith元素初始化为std::get<i>(other)...

Requires that sizeof...(Types) == sizeof...(UTypes) This overload only participates in overload resolution if std::is_constructible::value is true for all i.(until C++17)
This overload only participates in overload resolution if sizeof...(Types) == sizeof...(UTypes) and std::is_constructible_v<Ti, const Ui&> is true for all i and sizeof...(Types) != 1 or (when Types... expands to T and UTypes... expands to U) std::is_convertible_v<const tuple<U>&, T>, std::is_constructible_v<T, const tuple<U>&>, and std::is_same_v<T, U> are all false . The constructor is explicit if and only if std::is_convertible<const Ui&, Ti>::value is false for at least one i.(since C++17)

5%29转换移动构造函数。为所有人i在sizeof...(UTypes),将元组的ith元素初始化为std::forward<Ui>(std::get<i>(other))...

Requires that sizeof...(Types) == sizeof...(UTypes) This overload only participates in overload resolution if std::is_constructible::value is true for all i.(until C++17)
This overload only participates in overload resolution if sizeof...(Types) == sizeof...(UTypes) and std::is_constructible_v<Ti, Ui&&> is true for all i and sizeof...(Types) != 1 or (when Types... expands to T and UTypes... expands to U) std::is_convertible_v<tuple<U>, T>, std::is_constructible_v<T, tuple<U>>, and std::is_same_v<T, U> are all false . The constructor is explicit if and only if std::is_convertible<Ui&&, Ti>::value is false for at least one i.(since C++17)

6%对29对复制构造函数。构造具有第一个元素的2元素元组。p.first的第二个元素p.second

Requires that sizeof...(Types) == 2 This overload only participates in overload resolution if std::is_constructible::value and std::is_constructible::value are both true(until C++17)
This overload only participates in overload resolution if sizeof...(Types) == 2 and std::is_constructible<T0,const U1&>::value and std::is_constructible<T1, const U2&>::value are both true The constructor is explicit if and only if std::is_convertible<const U1&, T0>::value or std::is_convertible<const U2&, T1>::value is false(since C++17)

7%对29对移动构造函数。构造具有第一个元素的2元素元组。std::forward<U1>(p.first)的第二个元素std::forward<U2>(p.second)

Requires that sizeof...(Types) == 2 This overload only participates in overload resolution if std::is_constructible::value and std::is_constructible::value are both true(until C++17)
This overload only participates in overload resolution if sizeof...(Types) == 2 and std::is_constructible<T0, U1&&>::value and std::is_constructible<T1, U2&&>::value are both true The constructor is explicit if and only if std::is_convertible<U1&&, T0>::value or std::convertible<U2&&, T1>::value is false(since C++17)

8%29隐式定义的复制构造函数。使用相应的元素初始化元组的每个元素。other...

此构造函数是constexpr如果它执行的每一个操作都是constexpr.为了空的元组std::tuple<>,是的constexpr...

要求std::is_copy_constructible<Ti>::value是true为所有人i...

9%29隐式定义的移动构造函数。初始化元组的每个ith元素。std::forward<Ui>(std::get<i>(other))...

此构造函数是constexpr如果它执行的每一个操作都是constexpr.为了空的元组std::tuple<>,是的constexpr...

要求std::is_move_constructible<Ti>::value是true为所有人i...

与%281-9%29相同的10-18%29,但每个元素都是由用途分配器构造,即分配器对象。a作为附加参数传递给下列元素的构造函数:std::uses_allocator<Ui, Alloc>::value是true...

参数

args-values used to initialize each element of the tuple
other-a tuple of values used to initialize each element of the tupe
p-pair of values used to initialize both elements of this 2-tuple
a-allocator to use in uses-allocator construction

注记

C++17中有条件地显式构造函数使使用列表初始化语法在复制初始化上下文中构造元组成为可能:

二次

std::tuple<int, int> foo_tuple() { return {1, -1}; // Error until C++17 return std::make_tuple(1, -1 // Always works }

二次

注意,如果列表中的某些元素不能隐式转换为目标元组的相应元素,则构造函数将显式化。

二次

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

二次

二次

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

二次

另见

make_tuplecreates a tuple object of the type defined by the argument types (function template)
tiecreates a tuple of lvalue references or unpacks a tuple into individual objects (function template)
forward_as_tuplecreates a tuple of rvalue references (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/tuple/tuple