Imported Upstream version 2.99.2
[platform/upstream/libsigc++.git] / sigc++ / tuple-utils / tuple_cdr.h
1 /* Copyright (C) 2016 Murray Cumming
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  *  (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/
15  */
16
17 #ifndef SIGC_TUPLE_UTILS_TUPLE_CDR_H
18 #define SIGC_TUPLE_UTILS_TUPLE_CDR_H
19
20 #include <tuple>
21 #include <type_traits>
22 #include <utility>
23
24 namespace sigc {
25
26 namespace internal {
27
28 /**
29  * Get the type of a tuple without the first item.
30  */
31 template <typename T>
32 struct tuple_type_cdr; // primary template is not defined
33
34 // Partial specialization for tuples of at least one element:
35 template <typename H, typename... T>
36 struct tuple_type_cdr<std::tuple<H, T...>>
37 {
38   using type = std::tuple<T...>;
39 };
40
41 namespace detail {
42
43 template <typename T, std::size_t... I>
44 constexpr
45 decltype(auto)
46 tuple_cdr_impl(T&& t, std::index_sequence<0, I...>)
47 {
48   using cdr = typename tuple_type_cdr<std::decay_t<T>>::type;
49   return cdr(std::get<I>(std::forward<T>(t))...);
50 }
51
52 } // detail namespace
53
54 /**
55  * Get the a tuple without the first item.
56  * This is analogous to std::tuple_cat().
57  */
58 template <typename T>
59 constexpr
60 decltype(auto)
61 tuple_cdr(T&& t) {
62   //We use std::decay_t<> because tuple_size is not defined for references.
63   constexpr auto size = std::tuple_size<std::decay_t<T>>::value;
64
65   static_assert(size != 0, "tuple size must be non-zero");
66   using seq = std::make_index_sequence<size>;
67   return detail::tuple_cdr_impl(std::forward<T>(t), seq{});
68 }
69
70 } // namespace internal
71
72 } // namespace sigc
73
74 #endif //SIGC_TUPLE_UTILS_TUPLE_CDR_H