CKTIS / C++
std::make_tuple — creating a tuple with deduced types
std::make_tuple from <tuple> creates a tuple whose element types are deduced from its arguments. It normally stores values, but std::ref and std::cref let you create reference elements instead of copies.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <tuple>
template<class... TTypes>
constexpr std::tuple<std::unwrap_ref_decay_t<TTypes>...>
make_tuple(TTypes&&... t);
std::make_tuple(args...)std::make_tuple(args...) from <tuple> creates a std::tuple without requiring you to spell out its element types. Each argument initializes the corresponding element; ordinary lvalues are copied, while rvalues can be moved when their type supports it.
Each element type is determined by std::unwrap_ref_decay_t<TTypes>. First, any reference is removed. If the remaining type is an array, the result is a pointer to its element type, preserving the element type’s qualifiers. If it is a function type, the result is a pointer to that function type. Only in the remaining cases are top-level const and volatile qualifiers removed. For example, an ordinary string literal is an array of const char, so the tuple element has type const char*, not char* or std::string.
After that transformation, std::reference_wrapper<T> is unwrapped to T&. Use std::ref(x) to retain a reference to x, or std::cref(x) to retain a reference to a const object. The tuple does not extend the lifetime of the object referred to by such a reference; that object must still exist whenever the reference is used.
Unlike std::make_tuple, std::tie creates a tuple of lvalue references, while std::forward_as_tuple creates a tuple of references for forwarding arguments and requires particular care with temporary objects. In the example, the first element retains a copy of 7, whereas the second refers to value. The call to std::get<1>(data) only returns a reference to value; it is the += 1 operation that increments value through that reference.
Examples
#include <functional>
#include <iostream>
#include <tuple>
#include <type_traits>
int main() {
int value = 7;
auto data = std::make_tuple(value, std::ref(value));
static_assert(std::is_same_v<decltype(data), std::tuple<int, int&>>);
value = 9;
std::get<1>(data) += 1;
std::cout << std::get<0>(data) << ' '
<< std::get<1>(data) << ' ' << value << '\n';
}
Result
7 10 10
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.