dune-common 3.0-git
utility.hh
Go to the documentation of this file.
1#ifndef DUNE_COMMON_STD_UTILITY_HH
2#define DUNE_COMMON_STD_UTILITY_HH
3
4#include <cstddef>
5
6#include <type_traits>
7#include <utility>
8
9namespace Dune
10{
11
12 namespace Std
13 {
14
15
16#if __cpp_lib_integer_sequence >= 201304
17
18 using std::integer_sequence;
19 using std::index_sequence;
20 using std::make_integer_sequence;
21 using std::make_index_sequence;
22 using std::index_sequence_for;
23
24#else // __cpp_lib_integer_sequence >= 201304
25
26 // integer_sequence
27 // ----------------
28
34 template< class T, T... Ints >
36 {
37 static_assert( std::is_integral< T >::value, "Template parameter T is required to be an integral type" );
38
39 public:
40
42 typedef T value_type;
43
45 static constexpr std::size_t size () { return sizeof...( Ints ); }
46 };
47
48
53 template< std::size_t... Ints >
54 using index_sequence = integer_sequence< std::size_t, Ints... >;
55
56#ifndef DOXYGEN
57
58 namespace impl {
59
60 template<typename T, T i, T n, T... indices>
61 struct _make_integer_sequence
62 : public _make_integer_sequence<T,i+1,n,indices...,i>
63 {};
64
65 template<typename T, T n, T... indices>
66 struct _make_integer_sequence<T,n,n,indices...>
67 {
68 using type = integer_sequence<T,indices...>;
69 };
70
71 }
72
73#endif // DOXYGEN
74
75 template<typename T, T n>
76 using make_integer_sequence = typename impl::_make_integer_sequence<T,0,n>::type;
77
78 template<std::size_t n>
80
81#ifndef DOXYGEN
82
83 namespace impl {
84
85 // This is a workaround for clang bug 14858 (https://llvm.org/bugs/show_bug.cgi?id=14858)
86 // in a template alias declaration, clang always deduces sizeof...(T) as 1, if the template
87 // alias is evaluated with an unpacked template parameter pack (instead of one that is explicitly
88 // constructed as a list of types at the call site. This is slightly braindead (and has been around
89 // since at least clang 3.0).
90 // As a workaround, we lift the computation into a struct definition.
91 template<typename... T>
92 struct _get_pack_length
93 : public std::integral_constant<std::size_t,sizeof...(T)>
94 {};
95
96 }
97
98#endif // DOXYGEN
99
100 template<typename... T>
101 using index_sequence_for = make_index_sequence<impl::_get_pack_length<T...>{}>;
102
103#endif // __cpp_lib_integer_sequence >= 201304
104
105
106 } // namespace Std
107
108} // namespace Dune
109
110#endif // #ifndef DUNE_COMMON_STD_UTILITY_HH
Dune namespace.
Definition alignment.hh:11
typename impl::_make_integer_sequence< T, 0, n >::type make_integer_sequence
Definition utility.hh:76
integer_sequence< std::size_t, Ints... > index_sequence
std::index_sequence as introduced in C++14
Definition utility.hh:54
make_integer_sequence< std::size_t, n > make_index_sequence
Definition utility.hh:79
make_index_sequence< impl::_get_pack_length< T... >{}> index_sequence_for
Definition utility.hh:101
an implementation of std::integer_sequence as introduced in C++14
Definition utility.hh:36
static constexpr std::size_t size()
return number of elements in sequence
Definition utility.hh:45
T value_type
value type
Definition utility.hh:42