Implementing tuple_find – A Constexpr-Compatible Algorithm for std::tuple in Modern C++

Most STL algorithms work great with homogeneous containers like std::vector or std::array. But what about heterogeneous ones like std::tuple? In this article, I present an implementation of tuple_find – a constexpr-friendly algorithm that searches for a specific value in a std::tuple and returns a reference to the found element, along with its position. It handles both const and non-const references and supports multiple matches by allowing the search to continue from a given index. This is the second part of a short series on algorithms for heterogeneous containers. The first part covered tuple_for_each, a generic iteration algorithm for std::tuple. Features of tuple_find: Works with std::tuple of arbitrary types Returns a reference to the found value (as std::variant inside std::optional) Preserves const-ness of the matched element Supports repeated search using an optional start index Fully constexpr capable (C++20+) Avoids dangling references through compile-time checks Example: Find and modify elements int a{20}, b{10}, c{10}, d{30}; auto tpl = std::tie(a, b, c, d); size_t idx = 0; while (true) { auto result = tuple_find(tpl, 10, idx); if (!result) break; auto& ref = std::get(*result); // non-const reference ref.first += 100; idx = ref.second + 1; } After running this, b and c will both be 110. Example: constexpr usage static constexpr int x{10}, y{20}, z{30}; constexpr auto tpl = std::tie(x, y, z); constexpr auto result = tuple_find(tpl, 10); static_assert(result && std::get(*result).second == 0); This post gives a brief overview of the idea and usage.

Mar 25, 2025 - 18:21
 0
Implementing tuple_find – A Constexpr-Compatible Algorithm for std::tuple in Modern C++

Most STL algorithms work great with homogeneous containers like std::vector or std::array. But what about heterogeneous ones like std::tuple?

In this article, I present an implementation of tuple_find – a constexpr-friendly algorithm that searches for a specific value in a std::tuple and returns a reference to the found element, along with its position. It handles both const and non-const references and supports multiple matches by allowing the search to continue from a given index.

This is the second part of a short series on algorithms for heterogeneous containers. The first part covered tuple_for_each, a generic iteration algorithm for std::tuple.

Features of tuple_find:

  • Works with std::tuple of arbitrary types

  • Returns a reference to the found value (as std::variant inside std::optional)

  • Preserves const-ness of the matched element

  • Supports repeated search using an optional start index

  • Fully constexpr capable (C++20+)

  • Avoids dangling references through compile-time checks

Example: Find and modify elements

int a{20}, b{10}, c{10}, d{30};
auto tpl = std::tie(a, b, c, d);

size_t idx = 0;
while (true) {
  auto result = tuple_find(tpl, 10, idx);
  if (!result) break;
  auto& ref = std::get<0>(*result); // non-const reference
  ref.first += 100;
  idx = ref.second + 1;
}

After running this, b and c will both be 110.

Example: constexpr usage

static constexpr int x{10}, y{20}, z{30};
constexpr auto tpl = std::tie(x, y, z);
constexpr auto result = tuple_find(tpl, 10);

static_assert(result && std::get<1>(*result).second == 0);

This post gives a brief overview of the idea and usage.