Given a tuple, you can extract the type of the Nth element in the tuple with the help of std::tuple_element_t:
using example = std::tuple_element_t<1,
std::tuple
The index is zero-based, so element 0 is int, element 1 is char, and element 2 is float.
What about going in reverse? Given a tuple, find the index of a specific type.
templatestruct tuple_element_index_helper;
We start by writing a tuple_element_index_helper which does the heavy lifting. It sets value equal to the index of the first element whose type matches, or equal to the number of elements (i.e., one greater than the maximum legal element index) if the type was not found.
struct tuple_element_index_helper
{
static constexpr std::size_t value = 0;
};
This is the base case. The type is not found in the empty tuple, so we set the value to zero.
struct tuple_element_index_helper
{
static constexpr std::size_t value = 0;
using RestTuple = std::tuple
static_assert(
tuple_element_index_helper
std::tuple_size_v
"type appears more than once in tuple");
};
This is the success case. The type is the first element in the tuple. Therefore, the value is zero (index zero). We also validate that the type is not present in the remaining types of the tuple. If the value for std::tuple is not equal to the size of the tuple, then that means that the type was found among the remaining types, so we raise a compile-time assertion failure.
struct tuple_element_index_helper
{
using RestTuple = std::tuple
static constexpr std::size_t value = 1 +
tuple_element_index_helper
};
And then we have the failure case. If the type does not match the first element in the tuple, then we search for the type in the remaining elements and add one to account for the fact that we removed the first type.
templatestruct tuple_element_index
{
static constexpr std::size_t value =
tuple_element_index_helper
static_assert(value < std::tuple_size_v
"type does not appear in tuple");
};
Now that the helper is written, we can write the real template class. It asks the helper to do the work and validates that the resulting value is less than the size of the tuple, meaning that the type was found. If not, then we complain with a compile-time assertion.
inline constexpr std::size_t tuple_element_index_v
= tuple_element_index
Finally, we create an alias type to reduce future typing.
Let's take it out for a spin.
// index = 1constexpr std::size_t index =
tuple_element_index_v
// error: type does not appear in tuple
constexpr std::size_t index =
tuple_element_index_v
// error: type appears more than once in tuple
constexpr std::size_t index =
tuple_element_index_v
All of these mundane tuple tricks will come in handy soon.
Bonus chatter: You might be tempted to try something like this:
templateconstexpr int tuple_element_index_helper()
{
for (int i = 0; i < std::tuple_size_v
if constexpr (std::is_same_v<
T, std::tuple_element_t>) {
return i;
}
}
return std::tuple_size_v
}
Unfortunately, it doesn't work because a variable i is not valid as a template non-type parameter, not even in a constexpr context.
10 comments
Discussion is closed. Login to edit/delete existing comments.
variable "i" not valid as a template non-type parameter but you can add recursion over index:
template
constexpr int tuple_element_index_helper()
{
if constexpr (index == std::tuple_size_v
return index;
} else {
return std::is_same_v
index : tuple_element_index_helper
}
}
template
constexpr int tuple_element_index() {
return tuple_element_index_helper<0, T, Tuple>();
}
2 weeks ago, I needed this code, and I came up with a similar solution to you. But I really wanted a solution that would take advantage of a fold expression. Sadly, I couldn't find a solution. AFAIK, both our solutions instantiate a struct for each T and each element of each tuple type, and that is expensive.
If you don't need to worry about tuples containing the same type multiple times, you can drop the static_asserts, which should reduce the number of instantiated classes that the compiler generates.
Just posted a solution that avoids compile time recursion by using a fold expression here https://www.reddit.com/r/cpp/comments/hoasne/avoiding_compile_time_recursion/?utm_source=share&utm_medium=web2x
The nice thing about tuples that contain same type multiple times is that you can introduce strong typing, e.g. instead of
typedef tuple < double, double, double > Point3Dyou can write
typedef tuple.
Exercise for the reader: Create an index sequence for those tuple elements of a particular type.
I think
for constexpris coming with the static reflection proposal.Does anything happen at runtime in a C++ program any more?
or will we get normal IDE experience anymore?
The question is different: will C++ compilation times be reasonable anymore?
this comment has been deleted.