about summary refs log tree commit diff
path: root/absl/utility
diff options
context:
space:
mode:
Diffstat (limited to 'absl/utility')
-rw-r--r--absl/utility/utility.h28
-rw-r--r--absl/utility/utility_test.cc31
2 files changed, 59 insertions, 0 deletions
diff --git a/absl/utility/utility.h b/absl/utility/utility.h
index 7686db1250ae..eef8fb41fe8e 100644
--- a/absl/utility/utility.h
+++ b/absl/utility/utility.h
@@ -25,6 +25,7 @@
 //   * index_sequence_for<Ts...>     == std::index_sequence_for<Ts...>
 //   * apply<Functor, Tuple>         == std::apply<Functor, Tuple>
 //   * exchange<T>                   == std::exchange<T>
+//   * make_from_tuple<T>            == std::make_from_tuple<T>
 //
 // This header file also provides the tag types `in_place_t`, `in_place_type_t`,
 // and `in_place_index_t`, as well as the constant `in_place`, and
@@ -315,6 +316,33 @@ T exchange(T& obj, U&& new_value) {
   return old_value;
 }
 
+namespace utility_internal {
+template <typename T, typename Tuple, size_t... I>
+T make_from_tuple_impl(Tuple&& tup, absl::index_sequence<I...>) {
+  return T(std::get<I>(std::forward<Tuple>(tup))...);
+}
+}  // namespace utility_internal
+
+// make_from_tuple
+//
+// Given the template parameter type `T` and a tuple of arguments
+// `std::tuple(arg0, arg1, ..., argN)` constructs an object of type `T` as if by
+// calling `T(arg0, arg1, ..., argN)`.
+//
+// Example:
+//
+//   std::tuple<const char*, size_t> args("hello world", 5);
+//   auto s = absl::make_from_tuple<std::string>(args);
+//   assert(s == "hello");
+//
+template <typename T, typename Tuple>
+constexpr T make_from_tuple(Tuple&& tup) {
+  return utility_internal::make_from_tuple_impl<T>(
+      std::forward<Tuple>(tup),
+      absl::make_index_sequence<
+          std::tuple_size<absl::decay_t<Tuple>>::value>{});
+}
+
 }  // namespace absl
 
 #endif  // ABSL_UTILITY_UTILITY_H_
diff --git a/absl/utility/utility_test.cc b/absl/utility/utility_test.cc
index 5a4972b60085..f044ad644a4d 100644
--- a/absl/utility/utility_test.cc
+++ b/absl/utility/utility_test.cc
@@ -341,5 +341,36 @@ TEST(ExchangeTest, MoveOnly) {
   EXPECT_EQ(1, *b);
 }
 
+TEST(MakeFromTupleTest, String) {
+  EXPECT_EQ(
+      absl::make_from_tuple<std::string>(std::make_tuple("hello world", 5)),
+      "hello");
+}
+
+TEST(MakeFromTupleTest, MoveOnlyParameter) {
+  struct S {
+    S(std::unique_ptr<int> n, std::unique_ptr<int> m) : value(*n + *m) {}
+    int value = 0;
+  };
+  auto tup =
+      std::make_tuple(absl::make_unique<int>(3), absl::make_unique<int>(4));
+  auto s = absl::make_from_tuple<S>(std::move(tup));
+  EXPECT_EQ(s.value, 7);
+}
+
+TEST(MakeFromTupleTest, NoParameters) {
+  struct S {
+    S() : value(1) {}
+    int value = 2;
+  };
+  EXPECT_EQ(absl::make_from_tuple<S>(std::make_tuple()).value, 1);
+}
+
+TEST(MakeFromTupleTest, Pair) {
+  EXPECT_EQ(
+      (absl::make_from_tuple<std::pair<bool, int>>(std::make_tuple(true, 17))),
+      std::make_pair(true, 17));
+}
+
 }  // namespace