1 // Copyright 2017 The Abseil Authors.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // This header file contains C++11 versions of standard <utility> header
16 // abstractions available within C++14 and C++17, and are designed to be drop-in
17 // replacement for code compliant with C++14 and C++17.
19 // The following abstractions are defined:
21 // * integer_sequence<T, Ints...> == std::integer_sequence<T, Ints...>
22 // * index_sequence<Ints...> == std::index_sequence<Ints...>
23 // * make_integer_sequence<T, N> == std::make_integer_sequence<T, N>
24 // * make_index_sequence<N> == std::make_index_sequence<N>
25 // * index_sequence_for<Ts...> == std::index_sequence_for<Ts...>
26 // * apply<Functor, Tuple> == std::apply<Functor, Tuple>
27 // * exchange<T> == std::exchange<T>
28 // * make_from_tuple<T> == std::make_from_tuple<T>
30 // This header file also provides the tag types `in_place_t`, `in_place_type_t`,
31 // and `in_place_index_t`, as well as the constant `in_place`, and
32 // `constexpr` `std::move()` and `std::forward()` implementations in C++11.
36 // https://en.cppreference.com/w/cpp/utility/integer_sequence
37 // https://en.cppreference.com/w/cpp/utility/apply
38 // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3658.html
40 #ifndef ABSL_UTILITY_UTILITY_H_
41 #define ABSL_UTILITY_UTILITY_H_
48 #include "absl/base/config.h"
49 #include "absl/base/internal/inline_variable.h"
50 #include "absl/base/internal/invoke.h"
51 #include "absl/meta/type_traits.h"
57 // Class template representing a compile-time integer sequence. An instantiation
58 // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
59 // type through its template arguments (which is a common need when
60 // working with C++11 variadic templates). `absl::integer_sequence` is designed
61 // to be a drop-in replacement for C++14's `std::integer_sequence`.
65 // template< class T, T... Ints >
66 // void user_function(integer_sequence<T, Ints...>);
70 // // user_function's `T` will be deduced to `int` and `Ints...`
71 // // will be deduced to `0, 1, 2, 3, 4`.
72 // user_function(make_integer_sequence<int, 5>());
74 template <typename T, T... Ints>
75 struct integer_sequence {
77 static constexpr size_t size() noexcept { return sizeof...(Ints); }
82 // A helper template for an `integer_sequence` of `size_t`,
83 // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
84 // `std::index_sequence`.
85 template <size_t... Ints>
86 using index_sequence = integer_sequence<size_t, Ints...>;
88 namespace utility_internal {
90 template <typename Seq, size_t SeqSize, size_t Rem>
93 // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
94 template <typename T, T... Ints, size_t SeqSize>
95 struct Extend<integer_sequence<T, Ints...>, SeqSize, 0> {
96 using type = integer_sequence<T, Ints..., (Ints + SeqSize)...>;
99 template <typename T, T... Ints, size_t SeqSize>
100 struct Extend<integer_sequence<T, Ints...>, SeqSize, 1> {
101 using type = integer_sequence<T, Ints..., (Ints + SeqSize)..., 2 * SeqSize>;
104 // Recursion helper for 'make_integer_sequence<T, N>'.
105 // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
106 template <typename T, size_t N>
109 typename Extend<typename Gen<T, N / 2>::type, N / 2, N % 2>::type;
112 template <typename T>
114 using type = integer_sequence<T>;
117 template <typename T>
118 struct InPlaceTypeTag {
119 explicit InPlaceTypeTag() = delete;
120 InPlaceTypeTag(const InPlaceTypeTag&) = delete;
121 InPlaceTypeTag& operator=(const InPlaceTypeTag&) = delete;
125 struct InPlaceIndexTag {
126 explicit InPlaceIndexTag() = delete;
127 InPlaceIndexTag(const InPlaceIndexTag&) = delete;
128 InPlaceIndexTag& operator=(const InPlaceIndexTag&) = delete;
131 } // namespace utility_internal
133 // Compile-time sequences of integers
135 // make_integer_sequence
137 // This template alias is equivalent to
138 // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
139 // replacement for C++14's `std::make_integer_sequence`.
140 template <typename T, T N>
141 using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
143 // make_index_sequence
145 // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
146 // and is designed to be a drop-in replacement for C++14's
147 // `std::make_index_sequence`.
149 using make_index_sequence = make_integer_sequence<size_t, N>;
151 // index_sequence_for
153 // Converts a typename pack into an index sequence of the same length, and
154 // is designed to be a drop-in replacement for C++14's
155 // `std::index_sequence_for()`
156 template <typename... Ts>
157 using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
161 #ifdef ABSL_HAVE_STD_OPTIONAL
163 using std::in_place_t;
166 #else // ABSL_HAVE_STD_OPTIONAL
170 // Tag type used to specify in-place construction, such as with
171 // `absl::optional`, designed to be a drop-in replacement for C++17's
172 // `std::in_place_t`.
173 struct in_place_t {};
175 ABSL_INTERNAL_INLINE_CONSTEXPR(in_place_t, in_place, {});
177 #endif // ABSL_HAVE_STD_OPTIONAL
179 #if defined(ABSL_HAVE_STD_ANY) || defined(ABSL_HAVE_STD_VARIANT)
180 using std::in_place_type;
181 using std::in_place_type_t;
186 // Tag type used for in-place construction when the type to construct needs to
187 // be specified, such as with `absl::any`, designed to be a drop-in replacement
188 // for C++17's `std::in_place_type_t`.
189 template <typename T>
190 using in_place_type_t = void (*)(utility_internal::InPlaceTypeTag<T>);
192 template <typename T>
193 void in_place_type(utility_internal::InPlaceTypeTag<T>) {}
194 #endif // ABSL_HAVE_STD_ANY || ABSL_HAVE_STD_VARIANT
196 #ifdef ABSL_HAVE_STD_VARIANT
197 using std::in_place_index;
198 using std::in_place_index_t;
203 // Tag type used for in-place construction when the type to construct needs to
204 // be specified, such as with `absl::any`, designed to be a drop-in replacement
205 // for C++17's `std::in_place_index_t`.
207 using in_place_index_t = void (*)(utility_internal::InPlaceIndexTag<I>);
210 void in_place_index(utility_internal::InPlaceIndexTag<I>) {}
211 #endif // ABSL_HAVE_STD_VARIANT
213 // Constexpr move and forward
217 // A constexpr version of `std::move()`, designed to be a drop-in replacement
218 // for C++14's `std::move()`.
219 template <typename T>
220 constexpr absl::remove_reference_t<T>&& move(T&& t) noexcept {
221 return static_cast<absl::remove_reference_t<T>&&>(t);
226 // A constexpr version of `std::forward()`, designed to be a drop-in replacement
227 // for C++14's `std::forward()`.
228 template <typename T>
229 constexpr T&& forward(
230 absl::remove_reference_t<T>& t) noexcept { // NOLINT(runtime/references)
231 return static_cast<T&&>(t);
234 namespace utility_internal {
235 // Helper method for expanding tuple into a called method.
236 template <typename Functor, typename Tuple, std::size_t... Indexes>
237 auto apply_helper(Functor&& functor, Tuple&& t, index_sequence<Indexes...>)
238 -> decltype(absl::base_internal::Invoke(
239 absl::forward<Functor>(functor),
240 std::get<Indexes>(absl::forward<Tuple>(t))...)) {
241 return absl::base_internal::Invoke(
242 absl::forward<Functor>(functor),
243 std::get<Indexes>(absl::forward<Tuple>(t))...);
246 } // namespace utility_internal
250 // Invokes a Callable using elements of a tuple as its arguments.
251 // Each element of the tuple corresponds to an argument of the call (in order).
252 // Both the Callable argument and the tuple argument are perfect-forwarded.
253 // For member-function Callables, the first tuple element acts as the `this`
254 // pointer. `absl::apply` is designed to be a drop-in replacement for C++17's
255 // `std::apply`. Unlike C++17's `std::apply`, this is not currently `constexpr`.
263 // void user_function1(int, std::string);
264 // void user_function2(std::unique_ptr<Foo>);
265 // auto user_lambda = [](int, int) {};
269 // std::tuple<int, std::string> tuple1(42, "bar");
270 // // Invokes the first user function on int, std::string.
271 // absl::apply(&user_function1, tuple1);
273 // std::tuple<std::unique_ptr<Foo>> tuple2(absl::make_unique<Foo>());
274 // // Invokes the user function that takes ownership of the unique
276 // absl::apply(&user_function2, std::move(tuple2));
278 // auto foo = absl::make_unique<Foo>();
279 // std::tuple<Foo*, int> tuple3(foo.get(), 42);
280 // // Invokes the method Bar on foo with one argument, 42.
281 // absl::apply(&Foo::Bar, tuple3);
283 // std::tuple<int, int> tuple4(8, 9);
284 // // Invokes a lambda.
285 // absl::apply(user_lambda, tuple4);
287 template <typename Functor, typename Tuple>
288 auto apply(Functor&& functor, Tuple&& t)
289 -> decltype(utility_internal::apply_helper(
290 absl::forward<Functor>(functor), absl::forward<Tuple>(t),
291 absl::make_index_sequence<std::tuple_size<
292 typename std::remove_reference<Tuple>::type>::value>{})) {
293 return utility_internal::apply_helper(
294 absl::forward<Functor>(functor), absl::forward<Tuple>(t),
295 absl::make_index_sequence<std::tuple_size<
296 typename std::remove_reference<Tuple>::type>::value>{});
301 // Replaces the value of `obj` with `new_value` and returns the old value of
302 // `obj`. `absl::exchange` is designed to be a drop-in replacement for C++14's
307 // Foo& operator=(Foo&& other) {
308 // ptr1_ = absl::exchange(other.ptr1_, nullptr);
309 // int1_ = absl::exchange(other.int1_, -1);
312 template <typename T, typename U = T>
313 T exchange(T& obj, U&& new_value) {
314 T old_value = absl::move(obj);
315 obj = absl::forward<U>(new_value);
319 namespace utility_internal {
320 template <typename T, typename Tuple, size_t... I>
321 T make_from_tuple_impl(Tuple&& tup, absl::index_sequence<I...>) {
322 return T(std::get<I>(std::forward<Tuple>(tup))...);
324 } // namespace utility_internal
328 // Given the template parameter type `T` and a tuple of arguments
329 // `std::tuple(arg0, arg1, ..., argN)` constructs an object of type `T` as if by
330 // calling `T(arg0, arg1, ..., argN)`.
334 // std::tuple<const char*, size_t> args("hello world", 5);
335 // auto s = absl::make_from_tuple<std::string>(args);
336 // assert(s == "hello");
338 template <typename T, typename Tuple>
339 constexpr T make_from_tuple(Tuple&& tup) {
340 return utility_internal::make_from_tuple_impl<T>(
341 std::forward<Tuple>(tup),
342 absl::make_index_sequence<
343 std::tuple_size<absl::decay_t<Tuple>>::value>{});
348 #endif // ABSL_UTILITY_UTILITY_H_