std::experimental::apply(3)
π std::experimental::apply(3) π
okay so this page is from the C++ Standard Libary manual, bestie β yes, thatβs the manual name, and the title is literally std::experimental::apply(3). We are so back. π¦
π NAME π
std::experimental::apply β literally std::experimental::apply. Thatβs the whole name, no cap. π
β¨ Synopsis β¨
The synopsis is giving:
Defined in header <experimental/tuple>
template< class F, class Tuple > (library fundamentals TS)
constexpr decltype(auto) apply(F&& f, Tuple&& t);
It invokes the Callable object f with a tuple of arguments. Thatβs the vibe. πΈ
π Parameters π
fβ the Callable object to be invoked, periodt.tβ the tuple whose elements to be used as arguments tof.
π Return value π
Whatβs returned by f. Thatβs it β whatever f gives back is the return value. π«Ά
π§ Possible implementation π§
Hereβs the possible implementation, and yes the // Note: std::invoke is a C++17 feature line is part of the page, so donβt come for me:
namespace detail
{
template<class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t,
std::index_sequence<I...>)
{
return std::invoke(std::forward<F>(f),
std::get<I>(std::forward<Tuple>(t))...);
// Note: std::invoke is a C++17 feature
}
} // namespace detail
template<class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
return detail::apply_impl(std::forward<F>(f),
std::forward<Tuple>(t),
std::make_index_sequence<std::tuple_size_v<std::decay_t<Tuple>>>{});
}
π¦ Example π¦
This is the example exactly as the page gives it, including that little // Run this code comment:
// Run this code
#include <iostream>
#include <tuple>
template<typename... Ts>
void print_tuple(const std::tuple<Ts...> &tuple)
{
std::apply([](const auto&... elem)
{
((std::cout << elem << '\n'), ...);
}, tuple);
}
int main()
{
const std::tuple<int, char> t = std::make_tuple(5, 'a');
print_tuple(t);
}
π Output π
Output is literally:
5
a
β See also β
make_tupleβ creates a tuple object of the type defined by the argument types. Tagged(C++11)and(function template).forward_as_tupleβ creates a tuple of forwarding references. Tagged(C++11)and(function template).
π Category π
- Noindexed pages
okay thatβs the whole page, bestie π«Ά β huge cute sign-off shoutout to the footer crew: source http://cppreference.com, version/date 2024.06.10, page std::experimental::apply(3). πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms