tuples  0.1
 All Classes Namespaces Files Functions Typedefs Friends Macros
tagged_tuple.hpp
Go to the documentation of this file.
1 
7 #include <cstddef>
8 #include <functional>
9 #include <ostream>
10 #include <type_traits>
11 #include <utility>
12 
13 namespace tuples {
14 
15 #if __cplusplus >= 201402L
16 #define TUPLES_LIB_CONSTEXPR_CXX_14 constexpr
17 #else
18 #define TUPLES_LIB_CONSTEXPR_CXX_14
19 #endif
20 
21 namespace tuples_detail {
22 
23 template <class T>
24 inline constexpr T&& forward(
25  typename std::remove_reference<T>::type& t) noexcept {
26  return static_cast<T&&>(t);
27 }
28 
29 template <class T>
30 inline constexpr T&& forward(
31  typename std::remove_reference<T>::type&& t) noexcept {
32  static_assert(!std::is_lvalue_reference<T>::value,
33  "cannot forward an rvalue as an lvalue");
34  return static_cast<T&&>(t);
35 }
36 
37 template <class T, T...>
38 struct value_list {};
39 
40 template <class...>
41 struct typelist {};
42 
43 template <typename... Ts>
44 struct make_void {
45  typedef void type;
46 };
47 template <typename... Ts>
48 using void_t = typename make_void<Ts...>::type;
49 
50 template <bool... Bs>
51 using all = typename std::is_same<
52  value_list<bool, Bs...>,
53  value_list<bool, (static_cast<void>(Bs), true)...>>::type;
54 
55 struct no_such_type {
56  no_such_type() = delete;
57  no_such_type(no_such_type const& /*unused*/) = delete;
58  no_such_type(no_such_type&& /*unused*/) = delete;
59  ~no_such_type() = delete;
60  no_such_type& operator=(no_such_type const& /*unused*/) = delete;
61  no_such_type operator=(no_such_type&& /*unused*/) = delete;
62 };
63 
64 namespace detail {
65 using std::swap;
66 
67 template <class T, class S,
68  bool = not std::is_void<T>::value and not std::is_void<S>::value>
69 struct is_swappable_with {
70  template <class L, class R>
71  static auto test_swap(int)
72  -> decltype(swap(std::declval<L&>(), std::declval<R&>()));
73  template <class L, class R>
74  static tuples::tuples_detail::no_such_type test_swap(...);
75 
76  static const bool value =
77  not std::is_same<decltype(test_swap<T, S>(0)),
78  tuples::tuples_detail::no_such_type>::value and
79  not std::is_same<decltype(test_swap<S, T>(0)),
80  tuples::tuples_detail::no_such_type>::value;
81 };
82 
83 template <class T, class S>
84 struct is_swappable_with<T, S, false> : std::false_type {};
85 } // namespace detail
86 
87 template <class T, class S>
88 using is_swappable_with = detail::is_swappable_with<T, S>;
89 
90 template <class T, class S, bool = is_swappable_with<T, S>::value>
91 struct is_nothrow_swappable_with {
92  private:
93  static constexpr bool check() {
94  using std::swap;
95  return noexcept(swap(std::declval<T&>(), std::declval<S&>())) and
96  noexcept(swap(std::declval<S&>(), std::declval<T&>()));
97  }
98 
99  public:
100  static const bool value = check();
101 };
102 
103 template <class T, class S>
104 struct is_nothrow_swappable_with<T, S, false> : std::false_type {};
105 
106 template <typename... Ts>
107 constexpr char swallow(Ts&&...) noexcept {
108  return '0';
109 }
110 } // namespace tuples_detail
111 
112 namespace tuples_detail {
113 template <class Tag,
114  bool Ebo = std::is_empty<typename Tag::type>::value &&
115  !__is_final(typename Tag::type)>
116 class tagged_tuple_leaf;
117 
118 template <class T, bool B>
119 void swap(tagged_tuple_leaf<T, B>& lhs, tagged_tuple_leaf<T, B>& rhs) noexcept(
120  is_nothrow_swappable_with<typename T::type, typename T::type>::value) {
121  using std::swap;
122  swap(lhs.get(), rhs.get());
123 }
124 
125 template <class Tag>
126 class tagged_tuple_leaf<Tag, false> {
127  using value_type = typename Tag::type;
128  value_type value_;
129 
130  template <class T>
131  static constexpr bool can_bind_reference() noexcept {
132  using rem_ref_value_type = typename std::remove_reference<value_type>::type;
133  using rem_ref_T = typename std::remove_reference<T>::type;
134  using is_lvalue_type = std::integral_constant<
135  bool,
136  std::is_lvalue_reference<T>::value or
137  std::is_same<std::reference_wrapper<rem_ref_value_type>,
138  rem_ref_T>::value or
139  std::is_same<std::reference_wrapper<typename std::remove_const<
140  rem_ref_value_type>::type>,
141  rem_ref_T>::value>;
142  return not std::is_reference<value_type>::value or
143  (std::is_lvalue_reference<value_type>::value and
144  is_lvalue_type::value) or
145  (std::is_rvalue_reference<value_type>::value and
146  not std::is_lvalue_reference<T>::value);
147  }
148 
149  public:
150  // Tested in constexpr context in Unit.tagged_tuple.Ebo
151  constexpr tagged_tuple_leaf() noexcept(
152  std::is_nothrow_default_constructible<value_type>::value)
153  : value_() {
154  static_assert(
155  !std::is_reference<value_type>::value,
156  "Cannot default construct a reference element in a tagged_tuple");
157  }
158 
159  template <class T,
160  typename std::enable_if<
161  !std::is_same<typename std::decay<T>::type,
162  tagged_tuple_leaf>::value &&
163  std::is_constructible<value_type, T&&>::value>::type* = nullptr>
164  constexpr explicit tagged_tuple_leaf(T&& t) noexcept(
165  std::is_nothrow_constructible<value_type, T&&>::value)
166  : value_(tuples_detail::forward<T>(t)) {
167  static_assert(can_bind_reference<T>(),
168  "Cannot construct an lvalue reference with an rvalue");
169  }
170 
171  constexpr tagged_tuple_leaf(tagged_tuple_leaf const& /*rhs*/) = default;
172  constexpr tagged_tuple_leaf(tagged_tuple_leaf&& /*rhs*/) = default;
173 
174 #if __cplusplus < 201402L
175  value_type& get() noexcept { return value_; }
176 #else
177  constexpr value_type& get() noexcept { return value_; }
178 #endif
179  constexpr const value_type& get() const noexcept { return value_; }
180 
181  bool swap(tagged_tuple_leaf& t) noexcept(
182  is_nothrow_swappable_with<tagged_tuple_leaf, tagged_tuple_leaf>::value) {
183  using std::swap;
184  swap(*this, t);
185  return false;
186  }
187 };
188 
189 template <class Tag>
190 class tagged_tuple_leaf<Tag, true> : private Tag::type {
191  using value_type = typename Tag::type;
192 
193  public:
194  constexpr tagged_tuple_leaf() noexcept(
195  std::is_nothrow_default_constructible<value_type>::value)
196  : value_type{} {}
197 
198  template <class T,
199  typename std::enable_if<
200  !std::is_same<typename std::decay<T>::type,
201  tagged_tuple_leaf>::value &&
202  std::is_constructible<value_type, T&&>::value>::type* = nullptr>
203  constexpr explicit tagged_tuple_leaf(T&& t) noexcept(
204  std::is_nothrow_constructible<value_type, T&&>::value)
205  : value_type(tuples_detail::forward<T>(t)) {}
206 
207  constexpr tagged_tuple_leaf(tagged_tuple_leaf const& /*rhs*/) = default;
208  constexpr tagged_tuple_leaf(tagged_tuple_leaf&& /*rhs*/) = default;
209 
210 #if __cplusplus < 201402L
211  value_type& get() noexcept { return static_cast<value_type&>(*this); }
212 #else
213  constexpr value_type& get() noexcept {
214  return static_cast<value_type&>(*this);
215  }
216 #endif
217 
218  constexpr const value_type& get() const noexcept {
219  return static_cast<const value_type&>(*this);
220  }
221 
222  bool swap(tagged_tuple_leaf& t) noexcept(
223  is_nothrow_swappable_with<tagged_tuple_leaf, tagged_tuple_leaf>::value) {
224  using std::swap;
225  swap(*this, t);
226  return false;
227  }
228 };
229 
230 struct disable_constructors {
231  static constexpr bool enable_default() noexcept { return false; }
232  static constexpr bool enable_explicit() noexcept { return false; }
233  static constexpr bool enable_implicit() noexcept { return false; }
234 };
235 } // namespace tuples_detail
236 
249 template <class... Tags>
251 
252 template <class Tag, class... Tags>
253 constexpr const typename Tag::type& get(
254  const tagged_tuple<Tags...>& t) noexcept;
255 template <class Tag, class... Tags>
256 constexpr typename Tag::type& get(tagged_tuple<Tags...>& t) noexcept;
257 template <class Tag, class... Tags>
258 constexpr const typename Tag::type&& get(
259  const tagged_tuple<Tags...>&& t) noexcept;
260 template <class Tag, class... Tags>
261 constexpr typename Tag::type&& get(tagged_tuple<Tags...>&& t) noexcept;
262 
266 template <class Tag>
267 using tag_type = typename Tag::type;
268 
269 template <class... Tags>
270 class tagged_tuple : private tuples_detail::tagged_tuple_leaf<Tags>... {
271  template <class... Args>
272  struct pack_is_tagged_tuple : std::false_type {};
273  template <class Arg>
274  struct pack_is_tagged_tuple<Arg>
275  : std::is_same<typename std::decay<Arg>::type, tagged_tuple> {};
276 
277  template <bool EnableConstructor, class Dummy = void>
278  struct args_constructor : tuples_detail::disable_constructors {};
279 
280  template <class Dummy>
281  struct args_constructor<true, Dummy> {
282  static constexpr bool enable_default() {
283  return tuples_detail::all<
284  std::is_default_constructible<tag_type<Tags>>::value...>::value;
285  }
286 
287  template <class... Ts>
288  static constexpr bool enable_explicit() noexcept {
289  return tuples_detail::all<
290  std::is_constructible<tuples_detail::tagged_tuple_leaf<Tags>,
291  Ts>::value...>::value and
292  not tuples_detail::all<
293  std::is_convertible<Ts, tag_type<Tags>>::value...>::value;
294  }
295  template <class... Ts>
296  static constexpr bool enable_implicit() noexcept {
297  return sizeof...(Ts) == sizeof...(Tags) and
298  tuples_detail::all<
299  std::is_constructible<tuples_detail::tagged_tuple_leaf<Tags>,
300  Ts>::value...>::value and
301  tuples_detail::all<
302  std::is_convertible<Ts, tag_type<Tags>>::value...>::value;
303  }
304  };
305 
306  template <bool EnableConstructor, bool = sizeof...(Tags) == 1,
307  class Dummy = void>
308  struct tuple_like_constructor : tuples_detail::disable_constructors {};
309 
310  template <class Dummy>
311  struct tuple_like_constructor<true, false, Dummy> {
312  template <class Tuple, class... Ts>
313  static constexpr bool enable_explicit() noexcept {
314  return not tuples_detail::all<
315  std::is_convertible<Ts, tag_type<Tags>>::value...>::value;
316  }
317 
318  template <class Tuple, class... Ts>
319  static constexpr bool enable_implicit() noexcept {
320  return tuples_detail::all<
321  std::is_convertible<Ts, tag_type<Tags>>::value...>::value;
322  }
323  };
324 
325  template <class Dummy>
326  struct tuple_like_constructor<true, true, Dummy> {
327  template <class Tuple, class... Ts>
328  static constexpr bool enable_explicit() noexcept {
329  return not tuples_detail::all<
330  std::is_convertible<Ts, tag_type<Tags>>::value...>::value and
331  (not tuples_detail::all<std::is_convertible<
332  Tuple, tag_type<Tags>>::value...>::value and
333  not tuples_detail::all<std::is_constructible<
334  tag_type<Tags>, Tuple>::value...>::value and
335  not tuples_detail::all<
336  std::is_same<tag_type<Tags>, Ts>::value...>::value);
337  }
338 
339  template <class Tuple, class... Ts>
340  static constexpr bool enable_implicit() noexcept {
341  return tuples_detail::all<
342  std::is_convertible<Ts, tag_type<Tags>>::value...>::value and
343  (not tuples_detail::all<std::is_convertible<
344  Tuple, tag_type<Tags>>::value...>::value and
345  not tuples_detail::all<std::is_constructible<
346  tag_type<Tags>, Tuple>::value...>::value and
347  not tuples_detail::all<
348  std::is_same<tag_type<Tags>, Ts>::value...>::value);
349  }
350  };
351 
352  // C++17 Draft 23.5.3.2 Assignment - helper aliases
353  using is_copy_assignable =
354  tuples_detail::all<std::is_copy_assignable<tag_type<Tags>>::value...>;
355  using is_nothrow_copy_assignable = tuples_detail::all<
356  std::is_nothrow_copy_assignable<tag_type<Tags>>::value...>;
357  using is_move_assignable =
358  tuples_detail::all<std::is_move_assignable<tag_type<Tags>>::value...>;
359  using is_nothrow_move_assignable = tuples_detail::all<
360  std::is_nothrow_move_assignable<tag_type<Tags>>::value...>;
361 
362  template <class Tag, class... LTags>
363  friend constexpr const typename Tag::type& get(
364  const tagged_tuple<LTags...>& t) noexcept;
365  template <class Tag, class... LTags>
366  friend constexpr typename Tag::type& get(tagged_tuple<LTags...>& t) noexcept;
367  template <class Tag, class... LTags>
368  friend constexpr const typename Tag::type&& get(
369  const tagged_tuple<LTags...>&& t) noexcept;
370  template <class Tag, class... LTags>
371  friend constexpr typename Tag::type&& get(
372  tagged_tuple<LTags...>&& t) noexcept;
373 
374  public:
375  // C++17 Draft 23.5.3.1 Construction
376  template <bool Dummy = true,
377  typename std::enable_if<
378  args_constructor<Dummy>::enable_default()>::type* = nullptr>
379  constexpr tagged_tuple() noexcept(
380  tuples_detail::all<std::is_nothrow_default_constructible<
381  tag_type<Tags>>::value...>::value) {}
382 
383  constexpr tagged_tuple(tagged_tuple const& /*rhs*/) = default;
384  constexpr tagged_tuple(tagged_tuple&& /*rhs*/) = default;
385 
386  template <
387  bool Dummy = true,
388  typename std::enable_if<args_constructor<Dummy>::template enable_explicit<
389  tag_type<Tags> const&...>()>::type* = nullptr>
390  constexpr explicit tagged_tuple(tag_type<Tags> const&... ts) noexcept(
391  tuples_detail::all<std::is_nothrow_copy_constructible<
392  tuples_detail::tagged_tuple_leaf<Tags>>::value...>::value)
393  : tuples_detail::tagged_tuple_leaf<Tags>(ts)... {}
394 
395  template <
396  bool Dummy = true,
397  typename std::enable_if<args_constructor<Dummy>::template enable_implicit<
398  tag_type<Tags> const&...>()>::type* = nullptr>
399  constexpr tagged_tuple(tag_type<Tags> const&... ts) noexcept(
400  tuples_detail::all<std::is_nothrow_copy_constructible<
401  tuples_detail::tagged_tuple_leaf<Tags>>::value...>::value)
402  : tuples_detail::tagged_tuple_leaf<Tags>(ts)... {}
403 
404  template <class... Us,
405  typename std::enable_if<
406  args_constructor<not pack_is_tagged_tuple<Us...>::value and
407  sizeof...(Us) == sizeof...(Tags)>::
408  template enable_explicit<Us&&...>()>::type* = nullptr>
409  constexpr explicit tagged_tuple(Us&&... us) noexcept(
410  tuples_detail::all<std::is_nothrow_constructible<
411  tuples_detail::tagged_tuple_leaf<Tags>, Us&&>::value...>::value)
412  : tuples_detail::tagged_tuple_leaf<Tags>(
413  tuples_detail::forward<Us>(us))... {}
414 
415  template <class... Us,
416  typename std::enable_if<
417  args_constructor<not pack_is_tagged_tuple<Us...>::value and
418  sizeof...(Us) == sizeof...(Tags)>::
419  template enable_implicit<Us&&...>()>::type* = nullptr>
420  constexpr tagged_tuple(Us&&... us) noexcept(
421  tuples_detail::all<std::is_nothrow_constructible<
422  tuples_detail::tagged_tuple_leaf<Tags>, Us&&>::value...>::value)
423  : tuples_detail::tagged_tuple_leaf<Tags>(
424  tuples_detail::forward<Us>(us))... {}
425 
426  template <
427  class... UTags,
428  typename std::enable_if<
429  tuple_like_constructor<
430  sizeof...(Tags) == sizeof...(UTags) and
431  tuples_detail::all<std::is_constructible<
432  tag_type<Tags>, tag_type<UTags> const&>::value...>::value>::
433  template enable_explicit<tagged_tuple<UTags...> const&,
434  tag_type<UTags>...>()>::type* = nullptr>
435  constexpr explicit tagged_tuple(tagged_tuple<UTags...> const& t) noexcept(
436  tuples_detail::all<std::is_nothrow_constructible<
437  tag_type<Tags>, tag_type<UTags> const&>::value...>::value)
438  : tuples_detail::tagged_tuple_leaf<Tags>(get<UTags>(t))... {}
439 
440  template <
441  class... UTags,
442  typename std::enable_if<
443  tuple_like_constructor<
444  sizeof...(Tags) == sizeof...(UTags) and
445  tuples_detail::all<std::is_constructible<
446  tag_type<Tags>, tag_type<UTags> const&>::value...>::value>::
447  template enable_implicit<tagged_tuple<UTags...> const&,
448  tag_type<UTags>...>()>::type* = nullptr>
449  constexpr tagged_tuple(tagged_tuple<UTags...> const& t) noexcept(
450  tuples_detail::all<std::is_nothrow_constructible<
451  tag_type<Tags>, tag_type<UTags> const&>::value...>::value)
452  : tuples_detail::tagged_tuple_leaf<Tags>(get<UTags>(t))... {}
453 
454  template <
455  class... UTags,
456  typename std::enable_if<
457  tuple_like_constructor<
458  sizeof...(Tags) == sizeof...(UTags) and
459  tuples_detail::all<std::is_constructible<
460  tag_type<Tags>, tag_type<UTags>&&>::value...>::value>::
461  template enable_explicit<tagged_tuple<UTags...>&&,
462  tag_type<UTags>...>()>::type* = nullptr>
463  constexpr explicit tagged_tuple(tagged_tuple<UTags...>&& t) noexcept(
464  tuples_detail::all<std::is_nothrow_constructible<
465  tag_type<Tags>, tag_type<UTags>&&>::value...>::value)
466  : tuples_detail::tagged_tuple_leaf<Tags>(
467  tuples_detail::forward<tag_type<UTags>>(get<UTags>(t)))... {}
468 
469  template <
470  class... UTags,
471  typename std::enable_if<
472  tuple_like_constructor<
473  sizeof...(Tags) == sizeof...(UTags) and
474  tuples_detail::all<std::is_constructible<
475  tag_type<Tags>, tag_type<UTags>&&>::value...>::value>::
476  template enable_implicit<tagged_tuple<UTags...>&&,
477  tag_type<UTags>...>()>::type* = nullptr>
478  constexpr tagged_tuple(tagged_tuple<UTags...>&& t) noexcept(
479  tuples_detail::all<std::is_nothrow_constructible<
480  tag_type<Tags>, tag_type<UTags>&&>::value...>::value)
481  : tuples_detail::tagged_tuple_leaf<Tags>(
482  tuples_detail::forward<tag_type<UTags>>(get<UTags>(t)))... {}
483 
484  // C++17 Draft 23.5.3.2 Assignment
486  typename std::conditional<is_copy_assignable::value, tagged_tuple,
487  tuples_detail::no_such_type>::type const&
488  t) noexcept(is_nothrow_copy_assignable::value) {
489  static_cast<void>(
490  tuples_detail::swallow((get<Tags>(*this) = get<Tags>(t))...));
491  return *this;
492  }
493 
495  typename std::conditional<is_move_assignable::value, tagged_tuple,
496  tuples_detail::no_such_type>::type&&
497  t) noexcept(is_nothrow_move_assignable::value) {
498  static_cast<void>(tuples_detail::swallow(
499  (get<Tags>(*this) =
500  tuples_detail::forward<tag_type<Tags>>(get<Tags>(t)))...));
501  return *this;
502  }
503 
504  template <class... UTags,
505  typename std::enable_if<
506  sizeof...(Tags) == sizeof...(UTags) and
507  tuples_detail::all<std::is_assignable<
509  tag_type<UTags> const&>::value...>::value>::type* = nullptr>
511  tuples_detail::all<std::is_nothrow_assignable<
512  tag_type<Tags>&, tag_type<UTags> const&>::value...>::value) {
513  static_cast<void>(
514  tuples_detail::swallow((get<Tags>(*this) = get<UTags>(t))...));
515  return *this;
516  }
517 
518  template <
519  class... UTags,
520  typename std::enable_if<
521  sizeof...(Tags) == sizeof...(UTags) and
522  tuples_detail::all<std::is_assignable<
523  tag_type<Tags>&, tag_type<UTags>&&>::value...>::value>::type* =
524  nullptr>
526  tuples_detail::all<std::is_nothrow_assignable<
527  tag_type<Tags>&, tag_type<UTags>&&>::value...>::value) {
528  static_cast<void>(tuples_detail::swallow(
529  (get<Tags>(*this) =
530  tuples_detail::forward<tag_type<UTags>>(get<UTags>(t)))...));
531  return *this;
532  }
533 
534  // C++17 Draft 23.5.3.3 swap
535  void swap(tagged_tuple& t) noexcept(
536  tuples_detail::all<tuples_detail::is_nothrow_swappable_with<
537  tuples_detail::tagged_tuple_leaf<Tags>,
538  tuples_detail::tagged_tuple_leaf<Tags>>::value...>::value) {
539  tuples_detail::swallow(tuples_detail::tagged_tuple_leaf<Tags>::swap(
540  static_cast<tuples_detail::tagged_tuple_leaf<Tags>&>(t))...);
541  }
542 };
543 
545 template <>
546 class tagged_tuple<> {
547  public:
548  tagged_tuple() noexcept {}
549  void swap(tagged_tuple& /*unused*/) noexcept {}
550 };
552 
553 // C++17 Draft 23.5.3.6 Tuple helper classes
558 template <class T>
559 struct tuple_size;
560 
562 template <class... Tags>
563 struct tuple_size<tagged_tuple<Tags...>>
564  : std::integral_constant<size_t, sizeof...(Tags)> {};
565 template <class... Tags>
566 struct tuple_size<const tagged_tuple<Tags...>>
567  : tuple_size<tagged_tuple<Tags...>> {};
568 template <class... Tags>
569 struct tuple_size<volatile tagged_tuple<Tags...>>
570  : tuple_size<tagged_tuple<Tags...>> {};
571 template <class... Tags>
572 struct tuple_size<const volatile tagged_tuple<Tags...>>
573  : tuple_size<tagged_tuple<Tags...>> {};
575 
576 // C++17 Draft 23.5.3.7 Element access
577 // @{
581 template <class Tag, class... Tags>
582 inline constexpr const typename Tag::type& get(
583  const tagged_tuple<Tags...>& t) noexcept {
584  return static_cast<const tuples_detail::tagged_tuple_leaf<Tag>&>(t).get();
585 }
586 template <class Tag, class... Tags>
587 inline constexpr typename Tag::type& get(tagged_tuple<Tags...>& t) noexcept {
588  return static_cast<tuples_detail::tagged_tuple_leaf<Tag>&>(t).get();
589 }
590 template <class Tag, class... Tags>
591 inline constexpr const typename Tag::type&& get(
592  const tagged_tuple<Tags...>&& t) noexcept {
593  return static_cast<const typename Tag::type&&>(
594  static_cast<const tuples_detail::tagged_tuple_leaf<Tag>&&>(t).get());
595 }
596 template <class Tag, class... Tags>
597 inline constexpr typename Tag::type&& get(tagged_tuple<Tags...>&& t) noexcept {
598  return static_cast<typename Tag::type&&>(
599  static_cast<tuples_detail::tagged_tuple_leaf<Tag>&&>(t).get());
600 }
601 // @}
602 
603 // C++17 Draft 23.5.3.8 Relational operators
604 namespace tuples_detail {
605 struct equal {
606  template <class T, class U>
607  static TUPLES_LIB_CONSTEXPR_CXX_14 void apply(
608  T const& lhs, U const& rhs, bool& result) noexcept(noexcept(lhs == rhs)) {
609  result = result and lhs == rhs;
610  }
611 };
612 
613 template <class... LTags, class... RTags>
614 TUPLES_LIB_CONSTEXPR_CXX_14 bool tuple_equal_impl(
615  tagged_tuple<LTags...> const& lhs,
616  tagged_tuple<RTags...> const&
617  rhs) noexcept(noexcept(std::initializer_list<bool>{
618  (get<LTags>(lhs) == get<RTags>(rhs))...})) {
619  bool equal = true;
620  // This short circuits in the sense that the operator== is only evaluated if
621  // the result thus far is true
622  static_cast<void>(std::initializer_list<char>{
623  (equal::apply(get<LTags>(lhs), get<RTags>(rhs), equal), '0')...});
624  return equal;
625 }
626 } // namespace tuples_detail
627 
628 template <class... LTags, class... RTags,
629  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
630  nullptr>
632  tagged_tuple<LTags...> const& lhs,
634  rhs) noexcept(noexcept(tuples_detail::tuple_equal_impl(lhs, rhs))) {
635  return tuples_detail::tuple_equal_impl(lhs, rhs);
636 }
637 
638 template <class... LTags, class... RTags,
639  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
640  nullptr>
642  tagged_tuple<LTags...> const& lhs,
643  tagged_tuple<RTags...> const& rhs) noexcept(noexcept(lhs == rhs)) {
644  return not(lhs == rhs);
645 }
646 
647 namespace tuples_detail {
648 struct less {
649  template <class T, class U>
650  static TUPLES_LIB_CONSTEXPR_CXX_14 void apply(
651  T const& lhs, U const& rhs, bool& last_rhs_less_lhs,
652  bool& result) noexcept(noexcept(lhs < rhs) and noexcept(rhs < lhs)) {
653  if (result or last_rhs_less_lhs) {
654  return;
655  }
656  result = lhs < rhs;
657  if (result) {
658  return;
659  }
660  last_rhs_less_lhs = rhs < lhs;
661  }
662 };
663 
664 template <class... LTags, class... RTags>
665 TUPLES_LIB_CONSTEXPR_CXX_14 bool tuple_less_impl(
666  tagged_tuple<LTags...> const& lhs,
667  tagged_tuple<RTags...> const&
668  rhs) noexcept(noexcept(std::initializer_list<bool>{
669  (get<LTags>(lhs) < get<RTags>(rhs))...})) {
670  bool result = false;
671  bool last_rhs_less_lhs = false;
672  static_cast<void>(std::initializer_list<char>{
673  (less::apply(get<LTags>(lhs), get<RTags>(rhs), last_rhs_less_lhs, result),
674  '0')...});
675  return result;
676 }
677 } // namespace tuples_detail
678 
679 template <class... LTags, class... RTags,
680  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
681  nullptr>
683  tagged_tuple<LTags...> const& lhs,
685  rhs) noexcept(noexcept(tuples_detail::tuple_less_impl(lhs, rhs))) {
686  return tuples_detail::tuple_less_impl(lhs, rhs);
687 }
688 
689 template <class... LTags, class... RTags,
690  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
691  nullptr>
693  tagged_tuple<LTags...> const& lhs,
694  tagged_tuple<RTags...> const& rhs) noexcept(noexcept(rhs < lhs)) {
695  return rhs < lhs;
696 }
697 
698 template <class... LTags, class... RTags,
699  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
700  nullptr>
702  tagged_tuple<LTags...> const& lhs,
703  tagged_tuple<RTags...> const& rhs) noexcept(noexcept(rhs < lhs)) {
704  return not(rhs < lhs);
705 }
706 
707 template <class... LTags, class... RTags,
708  typename std::enable_if<sizeof...(LTags) == sizeof...(RTags)>::type* =
709  nullptr>
711  tagged_tuple<LTags...> const& lhs,
712  tagged_tuple<RTags...> const& rhs) noexcept(noexcept(lhs < rhs)) {
713  return not(lhs < rhs);
714 }
715 
716 // C++17 Draft 23.5.3.3 swap
717 template <
718  class... Tags,
719  typename std::enable_if<tuples_detail::all<tuples_detail::is_swappable_with<
720  tuples_detail::tagged_tuple_leaf<Tags>,
721  tuples_detail::tagged_tuple_leaf<Tags>>::value...>::value>::type* =
722  nullptr>
724  tuples_detail::all<tuples_detail::is_nothrow_swappable_with<
725  tuples_detail::tagged_tuple_leaf<Tags>,
726  tuples_detail::tagged_tuple_leaf<Tags>>::value...>::value) {
727  lhs.swap(rhs);
728 }
729 
730 namespace tuples_detail {
731 template <class S, class T, class = void_t<>>
732 struct is_streamable : std::false_type {};
733 template <class S, class T>
734 struct is_streamable<
735  S, T,
736  void_t<decltype(std::declval<typename std::add_lvalue_reference<S>::type>()
737  << std::declval<T>()),
738  typename std::enable_if<not std::is_same<S, T>::value>::type>>
739  : std::true_type {};
740 
741 template <class S, class T>
742 using is_streamable_t = typename is_streamable<S, T>::type;
743 
744 struct stream_helper {
745  template <class T>
746  void operator()(std::ostream& os, const T& element, size_t& current_value,
747  const size_t num_tags, const std::true_type /*meta*/) {
748  os << element;
749  current_value++;
750  if (current_value < num_tags) {
751  os << std::string(", ");
752  }
753  }
754 
755  template <class T>
756  void operator()(std::ostream& os, const T& /*element*/, size_t& current_value,
757  const size_t num_tags, const std::false_type /*meta*/) {
758  os << std::string("NOT STREAMABLE");
759  current_value++;
760  if (current_value < num_tags) {
761  os << std::string(", ");
762  }
763  }
764 };
765 } // namespace tuples_detail
766 
767 template <class... Tags>
768 std::ostream& operator<<(std::ostream& os, const tagged_tuple<Tags...>& t) {
769  os << std::string("(");
770  size_t current_value = 0;
771  tuples_detail::stream_helper helper;
772  // With empty tagged_tuple's helper is unused
773  static_cast<void>(helper);
774  static_cast<void>(std::initializer_list<char>{
775  (helper(os, get<Tags>(t), current_value, sizeof...(Tags),
776  tuples_detail::is_streamable_t<std::ostream, tag_type<Tags>>{}),
777  '0')...});
778  return os << std::string(")");
779 }
780 
781 } // namespace tuples
tagged_tuple & operator=(typename std::conditional< is_copy_assignable::value, tagged_tuple, tuples_detail::no_such_type >::type const &t) noexcept(is_nothrow_copy_assignable::value)
Definition: tagged_tuple.hpp:485
constexpr bool operator>=(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(lhs< rhs))
Definition: tagged_tuple.hpp:710
constexpr tagged_tuple(tagged_tuple< UTags...> const &t) noexcept(tuples_detail::all< std::is_nothrow_constructible< tag_type< Tags >, tag_type< UTags > const & >::value...>::value)
Definition: tagged_tuple.hpp:435
void swap(tagged_tuple &t) noexcept(tuples_detail::all< tuples_detail::is_nothrow_swappable_with< tuples_detail::tagged_tuple_leaf< Tags >, tuples_detail::tagged_tuple_leaf< Tags >>::value...>::value)
Definition: tagged_tuple.hpp:535
constexpr tagged_tuple(tagged_tuple< UTags...> &&t) noexcept(tuples_detail::all< std::is_nothrow_constructible< tag_type< Tags >, tag_type< UTags > && >::value...>::value)
Definition: tagged_tuple.hpp:463
std::ostream & operator<<(std::ostream &os, const tagged_tuple< Tags...> &t)
Definition: tagged_tuple.hpp:768
tagged_tuple & operator=(tagged_tuple< UTags...> &&t) noexcept(tuples_detail::all< std::is_nothrow_assignable< tag_type< Tags > &, tag_type< UTags > && >::value...>::value)
Definition: tagged_tuple.hpp:525
typename Tag::type tag_type
Metafunction that returns the type of the Tag.
Definition: tagged_tuple.hpp:267
tagged_tuple & operator=(tagged_tuple< UTags...> const &t) noexcept(tuples_detail::all< std::is_nothrow_assignable< tag_type< Tags > &, tag_type< UTags > const & >::value...>::value)
Definition: tagged_tuple.hpp:510
constexpr bool operator<=(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(rhs< lhs))
Definition: tagged_tuple.hpp:701
friend constexpr const Tag::type & get(const tagged_tuple< LTags...> &t) noexcept
A compile time associative container between structs and data.
Definition: tagged_tuple.hpp:250
tagged_tuple & operator=(typename std::conditional< is_move_assignable::value, tagged_tuple, tuples_detail::no_such_type >::type &&t) noexcept(is_nothrow_move_assignable::value)
Definition: tagged_tuple.hpp:494
constexpr tagged_tuple() noexcept(tuples_detail::all< std::is_nothrow_default_constructible< tag_type< Tags >>::value...>::value)
Definition: tagged_tuple.hpp:379
#define TUPLES_LIB_CONSTEXPR_CXX_14
Definition: tagged_tuple.hpp:18
constexpr tagged_tuple(tag_type< Tags > const &...ts) noexcept(tuples_detail::all< std::is_nothrow_copy_constructible< tuples_detail::tagged_tuple_leaf< Tags >>::value...>::value)
Definition: tagged_tuple.hpp:390
constexpr bool operator==(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(tuples_detail::tuple_equal_impl(lhs, rhs)))
Definition: tagged_tuple.hpp:631
constexpr tagged_tuple(Us &&...us) noexcept(tuples_detail::all< std::is_nothrow_constructible< tuples_detail::tagged_tuple_leaf< Tags >, Us && >::value...>::value)
Definition: tagged_tuple.hpp:409
void swap(tagged_tuple< Tags...> &lhs, tagged_tuple< Tags...> &rhs) noexcept(tuples_detail::all< tuples_detail::is_nothrow_swappable_with< tuples_detail::tagged_tuple_leaf< Tags >, tuples_detail::tagged_tuple_leaf< Tags >>::value...>::value)
Definition: tagged_tuple.hpp:723
Has a member variable value equal to the number of elements in a tagged_tuple.
Definition: tagged_tuple.hpp:559
constexpr bool operator>(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(rhs< lhs))
Definition: tagged_tuple.hpp:692
constexpr bool operator<(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(tuples_detail::tuple_less_impl(lhs, rhs)))
Definition: tagged_tuple.hpp:682
constexpr bool operator!=(tagged_tuple< LTags...> const &lhs, tagged_tuple< RTags...> const &rhs) noexcept(noexcept(lhs==rhs))
Definition: tagged_tuple.hpp:641