dune-common  2.11
tuplevector.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_TUPLEVECTOR_HH
6 #define DUNE_COMMON_TUPLEVECTOR_HH
7 
8 #include <tuple>
9 #include <utility>
10 
11 #include <dune/common/indices.hh>
14 
15 
16 
23 namespace Dune
24 {
25 
26 
27 
33 template<class... T>
34 class TupleVector : public std::tuple<T...>
35 {
36  using Base = std::tuple<T...>;
37 
38 public:
39 
46  template<class... TT,
47  class = std::void_t<decltype(Base(std::declval<TT&&>()...))>>
48  constexpr TupleVector(TT&&... tt) :
49  Base(std::forward<TT>(tt)...)
50  {}
51 
54  constexpr TupleVector()
55  {}
56 
59  template<std::size_t i,
60  std::enable_if_t<(i < sizeof...(T)), int> = 0>
61  constexpr decltype(auto) operator[](const Dune::index_constant<i>&) const
62  {
63  return std::get<i>(*this);
64  }
65 
68  template<std::size_t i,
69  std::enable_if_t<(i < sizeof...(T)), int> = 0>
70  constexpr decltype(auto) operator[](const Dune::index_constant<i>&)
71  {
72  return std::get<i>(*this);
73  }
74 
76  static constexpr std::size_t size()
77  {
78  return std::tuple_size<Base>::value;
79  }
80 };
81 
85 
86 template<class... T>
87 TupleVector(T...) -> TupleVector<T...>;
88 
89 template<class T1, class T2>
90 TupleVector(std::pair<T1, T2>) -> TupleVector<T1, T2>;
91 
92 template<class... T>
93 TupleVector(std::tuple<T...>) -> TupleVector<T...>;
94 
95 template <class Alloc, class... T>
96 TupleVector(std::allocator_arg_t, Alloc, T...) -> TupleVector<T...>;
97 
98 template<class Alloc, class T1, class T2>
99 TupleVector(std::allocator_arg_t, Alloc, std::pair<T1, T2>) -> TupleVector<T1, T2>;
100 
101 template<class Alloc, class... T>
102 TupleVector(std::allocator_arg_t, Alloc, std::tuple<T...>) -> TupleVector<T...>;
103 
105 
106 
107 template<class... T>
108 constexpr auto makeTupleVector(T&&... t)
109 {
110  // The std::decay_t<T> is is a slight simplification,
111  // because std::reference_wrapper needs special care.
112  return TupleVector<std::decay_t<T>...>(std::forward<T>(t)...);
113 }
114 
115 
116 
117 } // namespace Dune
118 
119 namespace std
120 {
125  template <size_t i, typename... Args>
126  struct tuple_element<i,Dune::TupleVector<Args...> >
127  {
128  using type = typename std::tuple_element<i, std::tuple<Args...> >::type;
129  };
130 
135  template <typename... Args>
136  struct tuple_size<Dune::TupleVector<Args...> >
137  : std::integral_constant<std::size_t, sizeof...(Args)>
138  {};
139 }
140 
141 #endif // DUNE_COMMON_TUPLEVECTOR_HH
constexpr TupleVector(TT &&... tt)
Construct from a set of arguments.
Definition: tuplevector.hh:48
std::integral_constant< std::size_t, i > index_constant
An index constant with value i.
Definition: indices.hh:29
I i
Definition: hybridmultiindex.hh:328
Dune namespace
Definition: alignedallocator.hh:12
static constexpr std::size_t size()
Number of elements of the tuple.
Definition: tuplevector.hh:76
typename std::tuple_element< i, std::tuple< Args... > >::type type
Definition: tuplevector.hh:128
STL namespace.
Traits for type conversions and type information.
constexpr TupleVector()
Default constructor.
Definition: tuplevector.hh:54
A class augmenting std::tuple by element access via operator[].
Definition: tuplevector.hh:34