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
71 // forward declaration of cons
72 template<typename Head, typename Tail = null_type>
77 // Takes a pointer and routes all assignments to whatever it points to
79 struct assign_to_pointee
82 explicit assign_to_pointee(T* p) : ptr(p) {}
84 template<typename Other>
85 assign_to_pointee& operator=(const Other& other)
95 // Swallows any assignment
99 swallow_assign const& operator=(const T&) const
105 template <typename T> struct add_const_reference : add_reference<typename add_const<T>::type> {};
107 template <class MyTail>
110 // Each of vc6 and vc7 seem to require a different formulation
111 // of this return type
112 template <class H, class T>
113 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
114 static typename add_reference<typename add_const<T>::type>::type
116 static typename add_const_reference<T>::type
118 execute( cons<H,T> const& u, long )
125 struct init_tail<null_type>
128 static null_type execute( cons<H,null_type> const& u, long )
134 static null_type execute(U const&, ...)
139 template <class H, class T>
140 void execute( cons<H,T> const&, int);
143 template <class Other>
145 init_head( Other const& u, ... )
150 template <class H, class T>
151 typename add_reference<typename add_const<H>::type>::type
152 init_head( cons<H,T> const& u, int )
157 inline char**** init_head(null_type const&, int);
159 } // end of namespace detail
161 // cons builds a heterogenous list of types
162 template<typename Head, typename Tail>
165 typedef cons self_type;
166 typedef Head head_type;
167 typedef Tail tail_type;
170 typedef typename boost::add_reference<head_type>::type head_ref;
171 typedef typename boost::add_reference<tail_type>::type tail_ref;
172 typedef typename detail::add_const_reference<head_type>::type head_cref;
173 typedef typename detail::add_const_reference<tail_type>::type tail_cref;
178 head_ref get_head() { return head; }
179 tail_ref get_tail() { return tail; }
181 head_cref get_head() const { return head; }
182 tail_cref get_tail() const { return tail; }
184 cons() : head(), tail() {}
186 #if defined BOOST_MSVC
187 template<typename Tail>
188 cons(head_cref h /* = head_type() */, // causes MSVC 6.5 to barf.
189 const Tail& t) : head(h), tail(t.head, t.tail)
193 cons(head_cref h /* = head_type() */, // causes MSVC 6.5 to barf.
194 const null_type& t) : head(h), tail(t)
200 explicit cons(head_cref h, const T& t) :
201 head(h), tail(t.head, t.tail)
205 explicit cons(head_cref h = head_type(),
206 tail_cref t = tail_type()) :
214 : head(detail::init_head(u, 0))
215 , tail(detail::init_tail<Tail>::execute(u, 0L))
219 template<typename Other>
220 cons& operator=(const Other& other)
230 // Determines if the parameter is null_type
231 template<typename T> struct is_null_type { enum { RET = 0 }; };
232 template<> struct is_null_type<null_type> { enum { RET = 1 }; };
234 /* Build a cons structure from the given Head and Tail. If both are null_type,
236 template<typename Head, typename Tail>
240 enum { tail_is_null_type = is_null_type<Tail>::RET };
242 typedef cons<Head, Tail> RET;
246 struct build_cons<null_type, null_type>
248 typedef null_type RET;
251 // Map the N elements of a tuple into a cons list
254 typename T2 = null_type,
255 typename T3 = null_type,
256 typename T4 = null_type,
257 typename T5 = null_type,
258 typename T6 = null_type,
259 typename T7 = null_type,
260 typename T8 = null_type,
261 typename T9 = null_type,
262 typename T10 = null_type
264 struct map_tuple_to_cons
266 typedef typename detail::build_cons<T10, null_type >::RET cons10;
267 typedef typename detail::build_cons<T9, cons10>::RET cons9;
268 typedef typename detail::build_cons<T8, cons9>::RET cons8;
269 typedef typename detail::build_cons<T7, cons8>::RET cons7;
270 typedef typename detail::build_cons<T6, cons7>::RET cons6;
271 typedef typename detail::build_cons<T5, cons6>::RET cons5;
272 typedef typename detail::build_cons<T4, cons5>::RET cons4;
273 typedef typename detail::build_cons<T3, cons4>::RET cons3;
274 typedef typename detail::build_cons<T2, cons3>::RET cons2;
275 typedef typename detail::build_cons<T1, cons2>::RET cons1;
278 // Workaround the lack of partial specialization in some compilers
282 template<typename Tuple>
286 typedef typename Tuple::tail_type tail_type;
287 typedef _element_type<N-1> next_elt_type;
290 typedef typename _element_type<N-1>::template inner<tail_type>::RET RET;
295 struct _element_type<0>
297 template<typename Tuple>
300 typedef typename Tuple::head_type RET;
304 } // namespace detail
307 // Return the Nth type of the given Tuple
308 template<int N, typename Tuple>
312 typedef detail::_element_type<N> nth_type;
315 typedef typename nth_type::template inner<Tuple>::RET RET;
321 #if defined(BOOST_MSVC) && (BOOST_MSVC == 1300)
322 // special workaround for vc7:
325 struct reference_adder
335 struct reference_adder<true>
345 // Return a reference to the Nth type of the given Tuple
346 template<int N, typename Tuple>
350 typedef typename element<N, Tuple>::RET elt_type;
351 enum { is_ref = is_reference<elt_type>::value };
354 typedef reference_adder<is_ref>::rebind<elt_type>::type RET;
358 // Return a const reference to the Nth type of the given Tuple
359 template<int N, typename Tuple>
360 struct element_const_ref
363 typedef typename element<N, Tuple>::RET elt_type;
364 enum { is_ref = is_reference<elt_type>::value };
367 typedef reference_adder<is_ref>::rebind<const elt_type>::type RET;
373 // Return a reference to the Nth type of the given Tuple
374 template<int N, typename Tuple>
378 typedef typename element<N, Tuple>::RET elt_type;
381 typedef typename add_reference<elt_type>::type RET;
385 // Return a const reference to the Nth type of the given Tuple
386 template<int N, typename Tuple>
387 struct element_const_ref
390 typedef typename element<N, Tuple>::RET elt_type;
393 typedef typename add_reference<const elt_type>::type RET;
398 } // namespace detail
400 // Get length of this tuple
401 template<typename Tuple>
404 BOOST_STATIC_CONSTANT(int, value = 1 + length<typename Tuple::tail_type>::value);
407 template<> struct length<tuple<> > {
408 BOOST_STATIC_CONSTANT(int, value = 0);
412 struct length<null_type>
414 BOOST_STATIC_CONSTANT(int, value = 0);
419 // Reference the Nth element in a tuple and retrieve it with "get"
423 template<typename Head, typename Tail>
425 typename detail::element_ref<N, cons<Head, Tail> >::RET
426 get(cons<Head, Tail>& t)
428 return get_class<N-1>::get(t.tail);
431 template<typename Head, typename Tail>
433 typename detail::element_const_ref<N, cons<Head, Tail> >::RET
434 get(const cons<Head, Tail>& t)
436 return get_class<N-1>::get(t.tail);
443 template<typename Head, typename Tail>
445 typename add_reference<Head>::type
446 get(cons<Head, Tail>& t)
451 template<typename Head, typename Tail>
453 typename add_reference<const Head>::type
454 get(const cons<Head, Tail>& t)
460 } // namespace detail
476 public detail::map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>::cons1
479 typedef detail::map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> mapped_tuple;
480 typedef typename mapped_tuple::cons10 cons10;
481 typedef typename mapped_tuple::cons9 cons9;
482 typedef typename mapped_tuple::cons8 cons8;
483 typedef typename mapped_tuple::cons7 cons7;
484 typedef typename mapped_tuple::cons6 cons6;
485 typedef typename mapped_tuple::cons5 cons5;
486 typedef typename mapped_tuple::cons4 cons4;
487 typedef typename mapped_tuple::cons3 cons3;
488 typedef typename mapped_tuple::cons2 cons2;
489 typedef typename mapped_tuple::cons1 cons1;
491 typedef typename detail::add_const_reference<T1>::type t1_cref;
492 typedef typename detail::add_const_reference<T2>::type t2_cref;
493 typedef typename detail::add_const_reference<T3>::type t3_cref;
494 typedef typename detail::add_const_reference<T4>::type t4_cref;
495 typedef typename detail::add_const_reference<T5>::type t5_cref;
496 typedef typename detail::add_const_reference<T6>::type t6_cref;
497 typedef typename detail::add_const_reference<T7>::type t7_cref;
498 typedef typename detail::add_const_reference<T8>::type t8_cref;
499 typedef typename detail::add_const_reference<T9>::type t9_cref;
500 typedef typename detail::add_const_reference<T10>::type t10_cref;
502 typedef cons1 inherited;
503 typedef tuple self_type;
505 tuple() : cons1(T1(), cons2(T2(), cons3(T3(), cons4(T4(), cons5(T5(), cons6(T6(),cons7(T7(),cons8(T8(),cons9(T9(),cons10(T10()))))))))))
520 cons1(t1, cons2(t2, cons3(t3, cons4(t4, cons5(t5, cons6(t6,cons7(t7,cons8(t8,cons9(t9,cons10(t10))))))))))
524 explicit tuple(t1_cref t1)
525 : cons1(t1, cons2(T2(), cons3(T3(), cons4(T4(), cons5(T5(), cons6(T6(),cons7(T7(),cons8(T8(),cons9(T9(),cons10(T10()))))))))))
528 template<typename Head, typename Tail>
529 tuple(const cons<Head, Tail>& other) :
530 cons1(other.head, other.tail)
534 template<typename First, typename Second>
535 self_type& operator=(const std::pair<First, Second>& other)
537 this->head = other.first;
538 this->tail.head = other.second;
542 template<typename Head, typename Tail>
543 self_type& operator=(const cons<Head, Tail>& other)
545 this->head = other.head;
546 this->tail = other.tail;
554 template<int N> struct workaround_holder {};
556 } // namespace detail
558 template<int N, typename Head, typename Tail>
559 typename detail::element_ref<N, cons<Head, Tail> >::RET
560 get(cons<Head, Tail>& t, detail::workaround_holder<N>* = 0)
562 return detail::get_class<N>::get(t);
565 template<int N, typename Head, typename Tail>
566 typename detail::element_const_ref<N, cons<Head, Tail> >::RET
567 get(const cons<Head, Tail>& t, detail::workaround_holder<N>* = 0)
569 return detail::get_class<N>::get(t);
573 template<typename T1>
576 make_tuple(const T1& t1)
578 return tuple<T1>(t1);
582 template<typename T1, typename T2>
585 make_tuple(const T1& t1, const T2& t2)
587 return tuple<T1, T2>(t1, t2);
591 template<typename T1, typename T2, typename T3>
594 make_tuple(const T1& t1, const T2& t2, const T3& t3)
596 return tuple<T1, T2, T3>(t1, t2, t3);
600 template<typename T1, typename T2, typename T3, typename T4>
602 tuple<T1, T2, T3, T4>
603 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
605 return tuple<T1, T2, T3, T4>(t1, t2, t3, t4);
609 template<typename T1, typename T2, typename T3, typename T4, typename T5>
611 tuple<T1, T2, T3, T4, T5>
612 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
614 return tuple<T1, T2, T3, T4, T5>(t1, t2, t3, t4, t5);
618 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
620 tuple<T1, T2, T3, T4, T5, T6>
621 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6)
623 return tuple<T1, T2, T3, T4, T5, T6>(t1, t2, t3, t4, t5, t6);
627 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
629 tuple<T1, T2, T3, T4, T5, T6, T7>
630 make_tuple(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7)
632 return tuple<T1, T2, T3, T4, T5, T6, T7>(t1, t2, t3, t4, t5, t6, t7);
636 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
638 tuple<T1, T2, T3, T4, T5, T6, T7, T8>
639 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)
641 return tuple<T1, T2, T3, T4, T5, T6, T7, T8>(t1, t2, t3, t4, t5, t6, t7, t8);
645 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
647 tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>
648 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)
650 return tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9>(t1, t2, t3, t4, t5, t6, t7, t8, t9);
654 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
656 tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
657 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)
659 return tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
662 // Tie variables into a tuple
663 template<typename T1>
665 tuple<detail::assign_to_pointee<T1> >
668 return make_tuple(detail::assign_to_pointee<T1>(&t1));
671 // Tie variables into a tuple
672 template<typename T1, typename T2>
674 tuple<detail::assign_to_pointee<T1>,
675 detail::assign_to_pointee<T2> >
678 return make_tuple(detail::assign_to_pointee<T1>(&t1),
679 detail::assign_to_pointee<T2>(&t2));
682 // Tie variables into a tuple
683 template<typename T1, typename T2, typename T3>
685 tuple<detail::assign_to_pointee<T1>,
686 detail::assign_to_pointee<T2>,
687 detail::assign_to_pointee<T3> >
688 tie(T1& t1, T2& t2, T3& t3)
690 return make_tuple(detail::assign_to_pointee<T1>(&t1),
691 detail::assign_to_pointee<T2>(&t2),
692 detail::assign_to_pointee<T3>(&t3));
695 // Tie variables into a tuple
696 template<typename T1, typename T2, typename T3, typename T4>
698 tuple<detail::assign_to_pointee<T1>,
699 detail::assign_to_pointee<T2>,
700 detail::assign_to_pointee<T3>,
701 detail::assign_to_pointee<T4> >
702 tie(T1& t1, T2& t2, T3& t3, T4& t4)
704 return make_tuple(detail::assign_to_pointee<T1>(&t1),
705 detail::assign_to_pointee<T2>(&t2),
706 detail::assign_to_pointee<T3>(&t3),
707 detail::assign_to_pointee<T4>(&t4));
710 // Tie variables into a tuple
711 template<typename T1, typename T2, typename T3, typename T4, typename T5>
713 tuple<detail::assign_to_pointee<T1>,
714 detail::assign_to_pointee<T2>,
715 detail::assign_to_pointee<T3>,
716 detail::assign_to_pointee<T4>,
717 detail::assign_to_pointee<T5> >
718 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5)
720 return make_tuple(detail::assign_to_pointee<T1>(&t1),
721 detail::assign_to_pointee<T2>(&t2),
722 detail::assign_to_pointee<T3>(&t3),
723 detail::assign_to_pointee<T4>(&t4),
724 detail::assign_to_pointee<T5>(&t5));
727 // Tie variables into a tuple
728 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
730 tuple<detail::assign_to_pointee<T1>,
731 detail::assign_to_pointee<T2>,
732 detail::assign_to_pointee<T3>,
733 detail::assign_to_pointee<T4>,
734 detail::assign_to_pointee<T5>,
735 detail::assign_to_pointee<T6> >
736 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6)
738 return make_tuple(detail::assign_to_pointee<T1>(&t1),
739 detail::assign_to_pointee<T2>(&t2),
740 detail::assign_to_pointee<T3>(&t3),
741 detail::assign_to_pointee<T4>(&t4),
742 detail::assign_to_pointee<T5>(&t5),
743 detail::assign_to_pointee<T6>(&t6));
746 // Tie variables into a tuple
747 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
749 tuple<detail::assign_to_pointee<T1>,
750 detail::assign_to_pointee<T2>,
751 detail::assign_to_pointee<T3>,
752 detail::assign_to_pointee<T4>,
753 detail::assign_to_pointee<T5>,
754 detail::assign_to_pointee<T6>,
755 detail::assign_to_pointee<T7> >
756 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7)
758 return make_tuple(detail::assign_to_pointee<T1>(&t1),
759 detail::assign_to_pointee<T2>(&t2),
760 detail::assign_to_pointee<T3>(&t3),
761 detail::assign_to_pointee<T4>(&t4),
762 detail::assign_to_pointee<T5>(&t5),
763 detail::assign_to_pointee<T6>(&t6),
764 detail::assign_to_pointee<T7>(&t7));
767 // Tie variables into a tuple
768 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
770 tuple<detail::assign_to_pointee<T1>,
771 detail::assign_to_pointee<T2>,
772 detail::assign_to_pointee<T3>,
773 detail::assign_to_pointee<T4>,
774 detail::assign_to_pointee<T5>,
775 detail::assign_to_pointee<T6>,
776 detail::assign_to_pointee<T7>,
777 detail::assign_to_pointee<T8> >
778 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8)
780 return make_tuple(detail::assign_to_pointee<T1>(&t1),
781 detail::assign_to_pointee<T2>(&t2),
782 detail::assign_to_pointee<T3>(&t3),
783 detail::assign_to_pointee<T4>(&t4),
784 detail::assign_to_pointee<T5>(&t5),
785 detail::assign_to_pointee<T6>(&t6),
786 detail::assign_to_pointee<T7>(&t7),
787 detail::assign_to_pointee<T8>(&t8));
790 // Tie variables into a tuple
791 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
793 tuple<detail::assign_to_pointee<T1>,
794 detail::assign_to_pointee<T2>,
795 detail::assign_to_pointee<T3>,
796 detail::assign_to_pointee<T4>,
797 detail::assign_to_pointee<T5>,
798 detail::assign_to_pointee<T6>,
799 detail::assign_to_pointee<T7>,
800 detail::assign_to_pointee<T8>,
801 detail::assign_to_pointee<T9> >
802 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8, T9 &t9)
804 return make_tuple(detail::assign_to_pointee<T1>(&t1),
805 detail::assign_to_pointee<T2>(&t2),
806 detail::assign_to_pointee<T3>(&t3),
807 detail::assign_to_pointee<T4>(&t4),
808 detail::assign_to_pointee<T5>(&t5),
809 detail::assign_to_pointee<T6>(&t6),
810 detail::assign_to_pointee<T7>(&t7),
811 detail::assign_to_pointee<T8>(&t8),
812 detail::assign_to_pointee<T9>(&t9));
814 // Tie variables into a tuple
815 template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
817 tuple<detail::assign_to_pointee<T1>,
818 detail::assign_to_pointee<T2>,
819 detail::assign_to_pointee<T3>,
820 detail::assign_to_pointee<T4>,
821 detail::assign_to_pointee<T5>,
822 detail::assign_to_pointee<T6>,
823 detail::assign_to_pointee<T7>,
824 detail::assign_to_pointee<T8>,
825 detail::assign_to_pointee<T9>,
826 detail::assign_to_pointee<T10> >
827 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5 &t5, T6 &t6, T7 &t7, T8 &t8, T9 &t9, T10 &t10)
829 return make_tuple(detail::assign_to_pointee<T1>(&t1),
830 detail::assign_to_pointee<T2>(&t2),
831 detail::assign_to_pointee<T3>(&t3),
832 detail::assign_to_pointee<T4>(&t4),
833 detail::assign_to_pointee<T5>(&t5),
834 detail::assign_to_pointee<T6>(&t6),
835 detail::assign_to_pointee<T7>(&t7),
836 detail::assign_to_pointee<T8>(&t8),
837 detail::assign_to_pointee<T9>(&t9),
838 detail::assign_to_pointee<T10>(&t10));
840 // "ignore" allows tuple positions to be ignored when using "tie".
842 detail::swallow_assign const ignore = detail::swallow_assign();
844 } // namespace tuples
846 #endif // BOOST_TUPLE_BASIC_NO_PARTIAL_SPEC_HPP