std::tuple<Types...>::tuple
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <tuple> で定義
|
||
constexpr tuple(); |
(1) | (C++11以上) (条件付きexplicit) |
tuple( const Types&... args ); |
(2) | (C++11以上) (C++14以上ではconstexpr) (条件付きexplicit) |
template< class... UTypes > tuple( UTypes&&... args ); |
(3) | (C++11以上) (C++14以上ではconstexpr) (条件付きexplicit) |
template< class... UTypes > tuple( const tuple<UTypes...>& other ); |
(4) | (C++11以上) (C++14以上ではconstexpr) (条件付きexplicit) |
template <class... UTypes> tuple( tuple<UTypes...>&& other ); |
(5) | (C++11以上) (C++14以上ではconstexpr) (条件付きexplicit) |
template< class U1, class U2 > tuple( const pair<U1,U2>& p ); |
(6) | (C++11以上) (C++14以上ではconstexpr) (条件付きexplicit) |
template< class U1, class U2 > tuple( pair<U1,U2>&& p ); |
(7) | (C++11以上) (C++14以上ではconstexpr) (条件付き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++20以上ではconstexpr) (条件付きexplicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const Types&... args ); |
(11) | (C++11以上) (C++20以上ではconstexpr) (条件付きexplicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, UTypes&&... args ); |
(12) | (C++11以上) (C++20以上ではconstexpr) (条件付きexplicit) |
template <class Alloc, class... UTypes> tuple( std::allocator_arg_t, const Alloc& a, const tuple<UTypes...>& other ); |
(13) | (C++11以上) (C++20以上ではconstexpr) (条件付きexplicit) |
template< class Alloc, class... UTypes > tuple( std::allocator_arg_t, const Alloc& a, tuple<UTypes...>&& other ); |
(14) | (C++11以上) (C++20以上ではconstexpr) (条件付き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++20以上ではconstexpr) (条件付きexplicit) |
template< class Alloc, class U1, class U2 > tuple( std::allocator_arg_t, const Alloc& a, pair<U1, U2>&& p ); |
(16) | (C++11以上) (C++20以上ではconstexpr) (条件付きexplicit) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, const tuple& other ); |
(17) | (C++11以上) (C++20以上ではconstexpr) |
template< class Alloc > tuple( std::allocator_arg_t, const Alloc& a, tuple&& other ); |
(18) | (C++11以上) (C++20以上ではconstexpr) |
新しいタプルを構築します。
1) デフォルトコンストラクタ。 すべての要素は値初期化されます。
このオーバーロードは、すべての
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 から構築される、2要素のタプルを構築します。 このオーバーロードは、
sizeof...(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) から構築される、2要素のタプルを構築します。 このオーバーロードは、
sizeof...(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 であることが要求されます。10-18) (1-9) と同様ですが、各要素がアロケータ使用構築によって作成される点が異なります。 つまり、
std::uses_allocator<Ui, Alloc>::value が true である各要素のコンストラクタに、追加の引数として Allocator オブジェクト a が渡されます。引数
| 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 を作成します (関数テンプレート) |