- tuple[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp11[meta cpp]
namespace std {
template <class... Types>
tuple<Types&&...> forward_as_tuple(Types&&...) noexcept; // C++11
template <class... Types>
constexpr tuple<Types&&...> forward_as_tuple(Types&&...) noexcept; // C++14
}パラメータの元の型からなるtupleを生成する。左辺値参照型は左辺値参照型として、右辺値は右辺値参照として転送される。
パラメータの元の型からなるtupleオブジェクト
投げない
#include <tuple>
#include <string>
#include <type_traits>
int main()
{
// 一時オブジェクトからは右辺値参照のtupleが作られる
auto t1 = std::forward_as_tuple(1, 'a', std::string("Hello"));
static_assert(std::is_same<decltype(t1), std::tuple<int&&, char&&, std::string&&>>());
// 左辺値からは左辺値参照のtupleが作られる
int a = 1;
char b = 'a';
std::string c = "Hello";
auto t2 = std::forward_as_tuple(a, b, c);
static_assert(std::is_same<decltype(t2), std::tuple<int&, char&, std::string&>>());
}- std::forward_as_tuple[color ff0000]
- C++11
- Clang: ??
- GCC: 4.6.1 [mark verified]
- ICC: ??
- Visual C++:
forward_as_tupleは、ドラフト仕様の段階でpack_argumentsという名前で一時期表記されていた。
コンパイラのバージョンによっては、この名前での実装もありえる。
- LWG2275 - Why is forward_as_tuple not constexpr?
- LWG Issue 1384. Function
pack_argumentsis poorly named- C++11の策定時に、この関数の名前が
pack_argumentsからforward_as_tupleへ改められた
- C++11の策定時に、この関数の名前が
- LWG Issue 1386.
pack_argumentsoverly complex- C++11の策定時に、戻り値の型が引数の型ごとに導出する複雑な規定から、単純に
tuple<Types&&...>とする形へ簡略化された
- C++11の策定時に、戻り値の型が引数の型ごとに導出する複雑な規定から、単純に