Overview
tuple
provides a set of utilities for working with C++11 standard tuples, backporting some
of the features from C++14 to ensure compatibility with earlier versions of the language.
It includes various helper functions and templates.
Usage
Constraints |
The estd::get function relies on unique types within the tuple. If a tuple contains multiple elements of the same type, only the first occurrence will be accessed correctly. |
The for_each function requires the function f passed to it to be compatible with all elements of the tuple. |
Usage Guidance |
Ensure that the types within the tuple are unique when using estd::get to avoid ambiguity.|br| |
When using for_each, the function f` should be able to accept all types in the tuple as arguments. For example, if the tuple contains both int and std::string, f should be able to process both types. |
Example
The following example demonstrates retrieving tuple elements.
TEST(tuple, get)
{
auto const t = std::make_tuple(uint8_t(5), uint16_t(9));
EXPECT_EQ(5U, ::estd::get<uint8_t>(t));
EXPECT_EQ(9U, ::estd::get<uint16_t>(t));
}
This example shows applying a function to each tuple element.
TEST(tuple, for_each)
{
auto const t = std::make_tuple(uint8_t(5), uint16_t(10));
SumUp sum;
::estd::for_each(t, sum);
EXPECT_EQ(15U, sum.value);
}