1 // - tuple_basic_no_partial_spec.hpp -----------------------------------------
3 // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2001 Doug Gregor (gregod@rpi.edu)
5 // Copyright (C) 2001 Gary Powell (gary.powell@sierra.com)
7 // Permission to copy, use, sell and distribute this software is granted
8 // provided this copyright notice appears in all copies.
9 // Permission to modify the code and to distribute modified code is granted
10 // provided this copyright notice appears in all copies, and a notice
11 // that the code was modified is included with the copyright notice.
13 // This software is provided "as is" without express or implied warranty,
14 // and with no claim as to its suitability for any purpose.
16 // For more information, see http://www.boost.org or http://lambda.cs.utu.fi
19 // 14 02 01 Remove extra ';'. Also, fixed 10-parameter to make_tuple. (DG)
20 // 10 02 01 Fixed "null_type" constructors.
21 // Implemented comparison operators globally.
22 // Hide element_type_ref and element_type_const_ref.
24 // 09 02 01 Extended to tuples of length 10. Changed comparison for
26 // to the same used by std::pair<>, added cnull_type() (GP)
27 // 03 02 01 Initial Version from original tuple.hpp code by JJ. (DG)
29 // -----------------------------------------------------------------
31 #ifndef BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP
32 #define BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP
34 #include "boost/type_traits.hpp"
37 #if defined BOOST_MSVC
38 #pragma warning(disable:4518) // storage-class or type specifier(s) unexpected here; ignored
39 #pragma warning(disable:4181) // qualifier applied to reference type ignored
40 #pragma warning(disable:4227) // qualifier applied to reference type ignored
46 // null_type denotes the end of a list built with "cons"
50 null_type(const null_type&, const null_type&) {}
53 // a helper function to provide a const null_type type temporary
54 inline const null_type cnull_type() { return null_type(); }
56 // forward declaration of tuple
58 typename T1 = null_type,
59 typename T2 = null_type,
60 typename T3 = null_type,
61 typename T4 = null_type,
62 typename T5 = null_type,
63 typename T6 = null_type,
64 typename T7 = null_type,
65 typename T8 = null_type,
66 typename T9 = null_type,
67 typename T10 = null_type
73 // Takes a pointer and routes all assignments to whatever it points to
75 struct assign_to_pointee
78 explicit assign_to_pointee(T* p) : ptr(p) {}
80 template<typename Other>
81 assign_to_pointee& operator=(const Other& other)
91 // Swallows any assignment
95 swallow_assign& operator=(const T&)
101 template <typename T> struct add_const_reference : add_reference<typename add_const<T>::type> {};
102 } // end of namespace detail
104 // cons builds a heterogenous list of types
105 template<typename Head, typename Tail = null_type>
108 typedef cons self_type;
109 typedef Head head_type;
110 typedef Tail tail_type;
113 typedef typename boost::add_reference<head_type>::type head_ref;
114 typedef typename boost::add_reference<tail_type>::type tail_ref;
115 typedef typename detail::add_const_reference<head_type>::type head_cref;
116 typedef typename detail::add_const_reference<tail_type>::type tail_cref;
121 head_ref get_head() { return head; }
122 tail_ref get_tail() { return tail; }
124 head_cref get_head() const { return head; }
125 tail_cref get_tail() const { return tail; }
127 #if defined BOOST_MSVC
128 template<typename Tail>
129 explicit cons(head_cref h /* = head_type() */, // causes MSVC 6.5 to barf.
130 const Tail& t) : head(h), tail(t.head, t.tail)
134 explicit cons(head_cref h /* = head_type() */, // causes MSVC 6.5 to barf.
135 const null_type& t) : head(h), tail(t)
141 explicit cons(head_cref h, const T& t) :
142 head(h), tail(t.head, t.tail)
146 explicit cons(head_cref h = head_type(),
147 tail_cref t = tail_type()) :
154 template<typename Other>
155 cons& operator=(const Other& other)
165 // Determines if the parameter is null_type
166 template<typename T> struct is_null_type { enum { RET = 0 }; };
167 template<> struct is_null_type<null_type> { enum { RET = 1 }; };
169 /* Build a cons structure from the given Head and Tail. If both are null_type,
171 template<typename Head, typename Tail>
175 enum { tail_is_null_type = is_null_type<Tail>::RET };
177 typedef cons<Head, Tail> RET;
181 struct build_cons<null_type, null_type>
183 typedef null_type RET;
186 // Map the N elements of a tuple into a cons list
189 typename T2 = null_type,
190 typename T3 = null_type,
191 typename T4 = null_type,
192 typename T5 = null_type,
193 typename T6 = null_type,
194 typename T7 = null_type,
195 typename T8 = null_type,
196 typename T9 = null_type,
197 typename T10 = null_type
199 struct map_tuple_to_cons
201 typedef typename detail::build_cons<T10, null_type >::RET cons10;
202 typedef typename detail::build_cons<T9, cons10>::RET cons9;
203 typedef typename detail::build_cons<T8, cons9>::RET cons8;
204 typedef typename detail::build_cons<T7, cons8>::RET cons7;
205 typedef typename detail::build_cons<T6, cons7>::RET cons6;
206 typedef typename detail::build_cons<T5, cons6>::RET cons5;
207 typedef typename detail::build_cons<T4, cons5>::RET cons4;
208 typedef typename detail::build_cons<T3, cons4>::RET cons3;
209 typedef typename detail::build_cons<T2, cons3>::RET cons2;
210 typedef typename detail::build_cons<T1, cons2>::RET cons1;
213 // Workaround the lack of partial specialization in some compilers
217 template<typename Tuple>
221 typedef typename Tuple::tail_type tail_type;
222 typedef _element_type<N-1> next_elt_type;
225 typedef typename _element_type<N-1>::template inner<tail_type>::RET RET;
230 struct _element_type<0>
232 template<typename Tuple>
235 typedef typename Tuple::head_type RET;
239 } // namespace detail
242 // Return the Nth type of the given Tuple
243 template<int N, typename Tuple>
247 typedef detail::_element_type<N> nth_type;
250 typedef typename nth_type::template inner<Tuple>::RET RET;
256 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1300)
257 // special workaround for vc7:
260 struct reference_adder
270 struct reference_adder<true>
280 // Return a reference to the Nth type of the given Tuple
281 template<int N, typename Tuple>
285 typedef typename element<N, Tuple>::RET elt_type;
286 enum { is_ref = is_reference<elt_type>::value };
289 typedef reference_adder<is_ref>::rebind<elt_type>::type RET;
293 // Return a const reference to the Nth type of the given Tuple
294 template<int N, typename Tuple>
295 struct element_const_ref
298 typedef typename element<N, Tuple>::RET elt_type;
299 enum { is_ref = is_reference<elt_type>::value };
302 typedef reference_adder<is_ref>::rebind<const elt_type>::type RET;
308 // Return a reference to the Nth type of the given Tuple
309 template<int N, typename Tuple>
313 typedef typename element<N, Tuple>::RET elt_type;
316 typedef typename add_reference<elt_type>::type RET;
320 // Return a const reference to the Nth type of the given Tuple
321 template<int N, typename Tuple>
322 struct element_const_ref
325 typedef typename element<N, Tuple>::RET elt_type;
328 typedef typename add_reference<const elt_type>::type RET;
333 } // namespace detail
335 // Get length of this tuple
336 template<typename Tuple>
339 BOOST_STATIC_CONSTANT(int, value = 1 + length<typename Tuple::tail_type>::value);
342 template<> struct length<tuple<> > {
343 BOOST_STATIC_CONSTANT(int, value = 0);
347 struct length<null_type>
349 BOOST_STATIC_CONSTANT(int, value = 0);
354 // Reference the Nth element in a tuple and retrieve it with "get"
358 template<typename Head, typename Tail>
360 typename detail::element_ref<N, cons<Head, Tail> >::RET
361 get(cons<Head, Tail>& t)
363 return get_class<N-1>::get(t.tail);
366 template<typename Head, typename Tail>
368 typename detail::element_const_ref<N, cons<Head, Tail> >::RET
369 get(const cons<Head, Tail>& t)
371 return get_class<N-1>::get(t.tail);
378 template<typename Head, typename Tail>
380 typename add_reference<Head>::type
381 get(cons<Head, Tail>& t)
386 template<typename Head, typename Tail>
388 typename add_reference<const Head>::type
389 get(const cons<Head, Tail>& t)
395 } // namespace detail
411 public detail::map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>::cons1
414 typedef detail::map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> mapped_tuple;
415 typedef typename mapped_tuple::cons10 cons10;
416 typedef typename mapped_tuple::cons9 cons9;
417 typedef typename mapped_tuple::cons8 cons8;
418 typedef typename mapped_tuple::cons7 cons7;
419 typedef typename mapped_tuple::cons6 cons6;
420 typedef typename mapped_tuple::cons5 cons5;
421 typedef typename mapped_tuple::cons4 cons4;
422 typedef typename mapped_tuple::cons3 cons3;
423 typedef typename mapped_tuple::cons2 cons2;
424 typedef typename mapped_tuple::cons1 cons1;
426 typedef typename detail::add_const_reference<T1>::type t1_cref;
427 typedef typename detail::add_const_reference<T2>::type t2_cref;
428 typedef typename detail::add_const_reference<T3>::type t3_cref;
429 typedef typename detail::add_const_reference<T4>::type t4_cref;
430 typedef typename detail::add_const_reference<T5>::type t5_cref;
431 typedef typename detail::add_const_reference<T6>::type t6_cref;
432 typedef typename detail::add_const_reference<T7>::type t7_cref;
433 typedef typename detail::add_const_reference<T8>::type t8_cref;
434 typedef typename detail::add_const_reference<T9>::type t9_cref;
435 typedef typename detail::add_const_reference<T10>::type t10_cref;
437 typedef cons1 inherited;
438 typedef tuple self_type;
440 explicit tuple(t1_cref t1 = T1(),
451 cons1(t1, cons2(t2, cons3(t3, cons4(t4, cons5(t5, cons6(t6,cons7(t7,cons8(t8,cons9(t9,cons10(t10))))))))))
455 template<typename Head, typename Tail>
456 explicit tuple(const cons<Head, Tail>& other) :
457 cons1(other.head, other.tail)
461 template<typename First, typename Second>
462 self_type& operator=(const std::pair<First, Second>& other)
464 this->head = other.first;
465 this->tail.head = other.second;
469 template<typename Head, typename Tail>
470 self_type& operator=(const cons<Head, Tail>& other)
472 this->head = other.head;
473 this->tail = other.tail;
481 template<int N> struct workaround_holder {};
483 } // namespace detail
485 template<int N, typename Head, typename Tail>
486 typename detail::element_ref<N, cons<Head, Tail> >::RET
487 get(cons<Head, Tail>& t, detail::workaround_holder<N>* = 0)
489 return detail::get_class<N>::get(t);
492 template<int N, typename Head, typename Tail>
493 typename detail::element_const_ref<N, cons<Head, Tail> >::RET
494 get(const cons<Head, Tail>& t, detail::workaround_holder<N>* = 0)
496 return detail::get_class<N>::get(t);
500 template<typename T1>
503 make_tuple(const T1& t1)
505 return tuple<T1>(t1);
509 template<typename T1, typename T2>
512 make_tuple(const T1& t1, const T2& t2)
514 return tuple<T1, T2>(t1, t2);
518 template<typename T1, typename T2, typename T3>
521 make_tuple(const T1& t1, const T2& t2, const T3& t3)
523 return tuple<T1, T2, T3>(t1, t2, t3);
527 template<typename T1, typename T2, typename T3, typename T4>
529 tuple<T1, T2, T3, T4>
530 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
532 return tuple<T1, T2, T3, T4>(t1, t2, t3, t4);
536 template<typename T1, typename T2, typename T3, typename T4, typename T5>
538 tuple<T1, T2, T3, T4, T5>
539 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
541 return tuple<T1, T2, T3, T4, T5>(t1, t2, t3, t4, t5);
545 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
547 tuple<T1, T2, T3, T4, T5, T6>
548 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6)
550 return tuple<T1, T2, T3, T4, T5, T6>(t1, t2, t3, t4, t5, t6);
554 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
556 tuple<T1, T2, T3, T4, T5, T6, T7>
557 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7)
559 return tuple<T1, T2, T3, T4, T5, T6, T7>(t1, t2, t3, t4, t5, t6, t7);
563 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
565 tuple<T1, T2, T3, T4, T5, T6, T7, T8>
566 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8)
568 return tuple<T1, T2, T3, T4, T5, T6, T7, T8>(t1, t2, t3, t4, t5, t6, t7, t8);
572 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
574 tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>
575 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9)
577 return tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>(t1, t2, t3, t4, t5, t6, t7, t8, t9);
581 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
583 tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
584 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10)
586 return tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
589 // Tie variables into a tuple
590 template<typename T1>
592 tuple<detail::assign_to_pointee<T1> >
595 return make_tuple(detail::assign_to_pointee<T1>(&t1));
598 // Tie variables into a tuple
599 template<typename T1, typename T2>
601 tuple<detail::assign_to_pointee<T1>,
602 detail::assign_to_pointee<T2> >
605 return make_tuple(detail::assign_to_pointee<T1>(&t1),
606 detail::assign_to_pointee<T2>(&t2));
609 // Tie variables into a tuple
610 template<typename T1, typename T2, typename T3>
612 tuple<detail::assign_to_pointee<T1>,
613 detail::assign_to_pointee<T2>,
614 detail::assign_to_pointee<T3> >
615 tie(T1& t1, T2& t2, T3& t3)
617 return make_tuple(detail::assign_to_pointee<T1>(&t1),
618 detail::assign_to_pointee<T2>(&t2),
619 detail::assign_to_pointee<T3>(&t3));
622 // Tie variables into a tuple
623 template<typename T1, typename T2, typename T3, typename T4>
625 tuple<detail::assign_to_pointee<T1>,
626 detail::assign_to_pointee<T2>,
627 detail::assign_to_pointee<T3>,
628 detail::assign_to_pointee<T4> >
629 tie(T1& t1, T2& t2, T3& t3, T4& t4)
631 return make_tuple(detail::assign_to_pointee<T1>(&t1),
632 detail::assign_to_pointee<T2>(&t2),
633 detail::assign_to_pointee<T3>(&t3),
634 detail::assign_to_pointee<T4>(&t4));
637 // Tie variables into a tuple
638 template<typename T1, typename T2, typename T3, typename T4, typename T5>
640 tuple<detail::assign_to_pointee<T1>,
641 detail::assign_to_pointee<T2>,
642 detail::assign_to_pointee<T3>,
643 detail::assign_to_pointee<T4>,
644 detail::assign_to_pointee<T5> >
645 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5)
647 return make_tuple(detail::assign_to_pointee<T1>(&t1),
648 detail::assign_to_pointee<T2>(&t2),
649 detail::assign_to_pointee<T3>(&t3),
650 detail::assign_to_pointee<T4>(&t4),
651 detail::assign_to_pointee<T5>(&t5));
654 // Tie variables into a tuple
655 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
657 tuple<detail::assign_to_pointee<T1>,
658 detail::assign_to_pointee<T2>,
659 detail::assign_to_pointee<T3>,
660 detail::assign_to_pointee<T4>,
661 detail::assign_to_pointee<T5>,
662 detail::assign_to_pointee<T6> >
663 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6)
665 return make_tuple(detail::assign_to_pointee<T1>(&t1),
666 detail::assign_to_pointee<T2>(&t2),
667 detail::assign_to_pointee<T3>(&t3),
668 detail::assign_to_pointee<T4>(&t4),
669 detail::assign_to_pointee<T5>(&t5),
670 detail::assign_to_pointee<T6>(&t6));
673 // Tie variables into a tuple
674 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
676 tuple<detail::assign_to_pointee<T1>,
677 detail::assign_to_pointee<T2>,
678 detail::assign_to_pointee<T3>,
679 detail::assign_to_pointee<T4>,
680 detail::assign_to_pointee<T5>,
681 detail::assign_to_pointee<T6>,
682 detail::assign_to_pointee<T7> >
683 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7)
685 return make_tuple(detail::assign_to_pointee<T1>(&t1),
686 detail::assign_to_pointee<T2>(&t2),
687 detail::assign_to_pointee<T3>(&t3),
688 detail::assign_to_pointee<T4>(&t4),
689 detail::assign_to_pointee<T5>(&t5),
690 detail::assign_to_pointee<T6>(&t6),
691 detail::assign_to_pointee<T7>(&t7));
694 // Tie variables into a tuple
695 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
697 tuple<detail::assign_to_pointee<T1>,
698 detail::assign_to_pointee<T2>,
699 detail::assign_to_pointee<T3>,
700 detail::assign_to_pointee<T4>,
701 detail::assign_to_pointee<T5>,
702 detail::assign_to_pointee<T6>,
703 detail::assign_to_pointee<T7>,
704 detail::assign_to_pointee<T8> >
705 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8)
707 return make_tuple(detail::assign_to_pointee<T1>(&t1),
708 detail::assign_to_pointee<T2>(&t2),
709 detail::assign_to_pointee<T3>(&t3),
710 detail::assign_to_pointee<T4>(&t4),
711 detail::assign_to_pointee<T5>(&t5),
712 detail::assign_to_pointee<T6>(&t6),
713 detail::assign_to_pointee<T7>(&t7),
714 detail::assign_to_pointee<T8>(&t8));
717 // Tie variables into a tuple
718 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
720 tuple<detail::assign_to_pointee<T1>,
721 detail::assign_to_pointee<T2>,
722 detail::assign_to_pointee<T3>,
723 detail::assign_to_pointee<T4>,
724 detail::assign_to_pointee<T5>,
725 detail::assign_to_pointee<T6>,
726 detail::assign_to_pointee<T7>,
727 detail::assign_to_pointee<T8>,
728 detail::assign_to_pointee<T9> >
729 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8, T9 &t9)
731 return make_tuple(detail::assign_to_pointee<T1>(&t1),
732 detail::assign_to_pointee<T2>(&t2),
733 detail::assign_to_pointee<T3>(&t3),
734 detail::assign_to_pointee<T4>(&t4),
735 detail::assign_to_pointee<T5>(&t5),
736 detail::assign_to_pointee<T6>(&t6),
737 detail::assign_to_pointee<T7>(&t7),
738 detail::assign_to_pointee<T8>(&t8),
739 detail::assign_to_pointee<T9>(&t9));
741 // Tie variables into a tuple
742 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
744 tuple<detail::assign_to_pointee<T1>,
745 detail::assign_to_pointee<T2>,
746 detail::assign_to_pointee<T3>,
747 detail::assign_to_pointee<T4>,
748 detail::assign_to_pointee<T5>,
749 detail::assign_to_pointee<T6>,
750 detail::assign_to_pointee<T7>,
751 detail::assign_to_pointee<T8>,
752 detail::assign_to_pointee<T9>,
753 detail::assign_to_pointee<T10> >
754 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8, T9 &t9, T10 &t10)
756 return make_tuple(detail::assign_to_pointee<T1>(&t1),
757 detail::assign_to_pointee<T2>(&t2),
758 detail::assign_to_pointee<T3>(&t3),
759 detail::assign_to_pointee<T4>(&t4),
760 detail::assign_to_pointee<T5>(&t5),
761 detail::assign_to_pointee<T6>(&t6),
762 detail::assign_to_pointee<T7>(&t7),
763 detail::assign_to_pointee<T8>(&t8),
764 detail::assign_to_pointee<T9>(&t9),
765 detail::assign_to_pointee<T10>(&t10));
767 // "ignore" allows tuple positions to be ignored when using "tie".
769 detail::swallow_assign ignore;
772 } // namespace tuples
774 #endif // BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP