1 // tuple_basic.hpp -----------------------------------------------------
3 // Copyright (C) 1999, 2000 Jaakko Järvi (jaakko.jarvi@cs.utu.fi)
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
17 // This and that, Gary Powell.
18 // Fixed return types for get_head/get_tail
19 // ( and other bugs ) per suggestion of Jens Maurer
20 // simplified element type accessors + bug fix (Jeremy Siek)
21 // Several changes/additions according to suggestions by Doug Gregor,
22 // William Kempf, Vesa Karvonen, John Max Skaller, Ed Brey, Beman Davis,
25 // -----------------------------------------------------------------
27 #ifndef BOOST_TUPLE_BASIC_HPP
28 #define BOOST_TUPLE_BASIC_HPP
31 #include <utility> // needed for the assignment from pair to tuple
33 #include "boost/type_traits/cv_traits.hpp"
37 // -- null_type --------------------------------------------------------
40 // a helper function to provide a const null_type type temporary
43 inline const null_type cnull_type() { return null_type(); }
47 // - cons forward declaration -----------------------------------------------
48 template <class HT, class TT> struct cons;
51 // - tuple forward declaration -----------------------------------------------
53 class T0 = null_type, class T1 = null_type, class T2 = null_type,
54 class T3 = null_type, class T4 = null_type, class T5 = null_type,
55 class T6 = null_type, class T7 = null_type, class T8 = null_type,
59 // tuple_length forward declaration
60 template<class T> struct tuple_length;
67 // -- generate error template, referencing to non-existing members of this
68 // template is used to produce compilation errors intentionally
72 // tuple default argument wrappers ---------------------------------------
73 // Work for non-reference types, intentionally not for references
77 // Non-class temporaries cannot have qualifiers.
78 // To prevent f to return for example const int, we remove cv-qualifiers
79 // from all temporaries.
80 static typename boost::remove_cv<T>::type f() { return T(); }
84 struct default_arg<T&> {
86 return generate_error<T>::no_default_values_for_reference_types;
90 // - cons getters --------------------------------------------------------
91 // called: element<N>::get<RETURN_TYPE>(aTuple)
95 template<class RET, class HT, class TT >
96 inline static RET get(const cons<HT, TT>& t)
98 return element<N-1>::template get<RET>(t.tail);
100 template<class RET, class HT, class TT >
101 inline static RET get(cons<HT, TT>& t)
103 return element<N-1>::template get<RET>(t.tail);
109 template<class RET, class HT, class TT>
110 inline static RET get(const cons<HT, TT>& t)
114 template<class RET, class HT, class TT>
115 inline static RET get(cons<HT, TT>& t)
121 } // end of namespace tuples
122 } // end of namespace detail
125 // -cons type accessors ----------------------------------------
126 // typename tuple_element<N,T>::type gets the type of the
127 // Nth element ot T, first element is at index 0
128 // -------------------------------------------------------
130 template<int N, class T>
134 typedef typename T::tail_type Next;
136 typedef typename tuple_element<N-1, Next>::type type;
139 struct tuple_element<0,T>
141 typedef typename T::head_type type;
144 // -get function templates -----------------------------------------------
145 // Usage: get<N>(aTuple)
147 // -- some traits classes for get functions
149 // access traits lifted from detail namespace to be part of the interface,
150 // (Joel de Guzman's suggestion). Rationale: get functions are part of the
151 // interface, so should the way to express their return types be.
153 template <class T> struct tuple_access_traits {
154 typedef const T& const_type;
155 typedef T& non_const_type;
157 typedef const typename boost::remove_cv<T>::type& parameter_type;
158 // used as the tuple constructors parameter types
159 // Rationale: non-reference tuple element types can be cv-qualified.
160 // It should be possible to initialize such types with temporaries,
161 // and when binding temporaries to references, the reference must
162 // be non-volatile and const. 8.5.3. (5)
165 template <class T> struct tuple_access_traits<T&> {
167 typedef T& const_type;
168 typedef T& non_const_type;
170 typedef T& parameter_type;
174 // get function for non-const cons-lists, returns a reference to the element
176 template<int N, class HT, class TT>
177 inline typename tuple_access_traits<
178 typename tuple_element<N, cons<HT, TT> >::type
180 get(cons<HT, TT>& c) {
181 return detail::tuples::element<N>::template
183 typename tuple_access_traits<
184 typename tuple_element<N, cons<HT, TT> >::type
185 >::non_const_type>(c);
188 // get function for const cons-lists, returns a const reference to
189 // the element. If the element is a reference, returns the reference
190 // as such (that is, can return a non-const reference)
191 template<int N, class HT, class TT>
192 inline typename tuple_access_traits<
193 typename tuple_element<N, cons<HT, TT> >::type
195 get(const cons<HT, TT>& c) {
196 return detail::tuples::element<N>::template
198 typename tuple_access_traits<
199 typename tuple_element<N, cons<HT, TT> >::type
206 // -- the cons template --------------------------------------------------
208 template <class HT, class TT>
211 typedef HT head_type;
212 typedef TT tail_type;
217 typename tuple_access_traits<head_type>::non_const_type
218 get_head() { return head; }
220 typename tuple_access_traits<tail_type>::non_const_type
221 get_tail() { return tail; }
223 typename tuple_access_traits<head_type>::const_type
224 get_head() const { return head; }
226 typename tuple_access_traits<tail_type>::const_type
227 get_tail() const { return tail; }
229 cons(typename tuple_access_traits<head_type>::parameter_type h,
231 : head (h), tail(t) {}
233 template <class T1, class T2, class T3, class T4, class T5,
234 class T6, class T7, class T8, class T9, class T10>
235 cons( T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
236 T6& t6, T7& t7, T8& t8, T9& t9, T10& t10 )
238 tail (t2, t3, t4, t5, t6, t7, t8, t9, t10, detail::tuples::cnull_type())
241 template <class HT2, class TT2>
242 cons( const cons<HT2, TT2>& u ) : head(u.head), tail(u.tail) {}
244 template <class HT2, class TT2>
245 cons& operator=( const cons<HT2, TT2>& u ) {
246 head=u.head; tail=u.tail; return *this;
249 // must define assignment operator explicitly, implicit version is
250 // illformed if HT is a reference (12.8. (12))
251 cons& operator=(const cons& u) {
252 head = u.head; tail = u.tail; return *this;
255 template <class T1, class T2>
256 cons& operator=( const std::pair<T1, T2>& u ) {
257 BOOST_STATIC_ASSERT(tuple_length<cons>::value == 2); // check length = 2
258 head = u.first; tail.head = u.second; return *this;
261 // get member functions (non-const and const)
263 typename tuple_access_traits<
264 typename tuple_element<N, cons<HT, TT> >::type
267 return boost::get<N>(*this); // delegate to non-member get
271 typename tuple_access_traits<
272 typename tuple_element<N, cons<HT, TT> >::type
275 return boost::get<N>(*this); // delegate to non-member get
280 struct cons<HT, null_type> {
282 typedef HT head_type;
283 typedef null_type tail_type;
287 typename tuple_access_traits<head_type>::non_const_type
288 get_head() { return head; }
290 null_type get_tail() { return null_type(); }
292 typename tuple_access_traits<head_type>::const_type
293 get_head() const { return head; }
295 const null_type get_tail() const { return null_type(); }
297 cons(typename tuple_access_traits<head_type>::parameter_type h,
298 const null_type& = null_type())
302 cons(T1& t1, const null_type&, const null_type&, const null_type&,
303 const null_type&, const null_type&, const null_type&,
304 const null_type&, const null_type&, const null_type&)
308 cons( const cons<HT2, null_type>& u ) : head(u.head) {}
311 cons& operator=(const cons<HT2, null_type>& u )
312 { head = u.head; return *this; }
314 // must define assignment operator explicitely, implicit version
315 // is illformed if HT is a reference
316 cons& operator=(const cons& u) { head = u.head; return *this; }
319 typename tuple_access_traits<
320 typename tuple_element<N, cons>::type
323 return boost::get<N>(*this);
327 typename tuple_access_traits<
328 typename tuple_element<N, cons>::type
331 return boost::get<N>(*this);
336 // templates for finding out the length of the tuple -------------------
339 struct tuple_length {
340 BOOST_STATIC_CONSTANT(int, value = 1 + tuple_length<typename T::tail_type>::value);
344 struct tuple_length<null_type> {
345 BOOST_STATIC_CONSTANT(int, value = 0);
352 // Tuple to cons mapper --------------------------------------------------
353 template <class T0, class T1, class T2, class T3, class T4,
354 class T5, class T6, class T7, class T8, class T9>
355 struct map_tuple_to_cons
358 typename map_tuple_to_cons<T1, T2, T3, T4, T5,
359 T6, T7, T8, T9, null_type>::type
363 // The empty tuple is a null_type
365 struct map_tuple_to_cons<null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type>
367 typedef null_type type;
373 // -------------------------------------------------------------------
374 // -- tuple ------------------------------------------------------
375 template <class T0, class T1, class T2, class T3, class T4,
376 class T5, class T6, class T7, class T8, class T9>
379 public detail::tuples::map_tuple_to_cons<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type
383 detail::tuples::map_tuple_to_cons<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type inherited;
384 typedef typename inherited::head_type head_type;
385 typedef typename inherited::tail_type tail_type;
388 // tuple_access_traits<T>::parameter_type takes non-reference types as const T&
390 typename tuple_access_traits<T0>::parameter_type t0
391 = detail::tuples::default_arg<T0>::f(),
392 typename tuple_access_traits<T1>::parameter_type t1
393 = detail::tuples::default_arg<T1>::f(),
394 typename tuple_access_traits<T2>::parameter_type t2
395 = detail::tuples::default_arg<T2>::f(),
396 typename tuple_access_traits<T3>::parameter_type t3
397 = detail::tuples::default_arg<T3>::f(),
398 typename tuple_access_traits<T4>::parameter_type t4
399 = detail::tuples::default_arg<T4>::f(),
400 typename tuple_access_traits<T5>::parameter_type t5
401 = detail::tuples::default_arg<T5>::f(),
402 typename tuple_access_traits<T6>::parameter_type t6
403 = detail::tuples::default_arg<T6>::f(),
404 typename tuple_access_traits<T7>::parameter_type t7
405 = detail::tuples::default_arg<T7>::f(),
406 typename tuple_access_traits<T8>::parameter_type t8
407 = detail::tuples::default_arg<T8>::f(),
408 typename tuple_access_traits<T9>::parameter_type t9
409 = detail::tuples::default_arg<T9>::f())
411 : inherited(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) {}
413 template<class U1, class U2>
414 tuple(const cons<U1, U2>& p) : inherited(p) {}
416 template <class U1, class U2>
417 tuple& operator=(const cons<U1, U2>& k) {
418 inherited::operator=(k);
422 template <class U1, class U2>
423 tuple& operator=(const std::pair<U1, U2>& k) {
424 BOOST_STATIC_ASSERT(tuple_length<tuple>::value == 2);// check_length = 2
425 this->head = k.first;
426 this->tail.head = k.second;
434 class tuple<null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type, null_type> :
438 typedef null_type inherited;
442 // Swallows any assignment (by Doug Gregor)
446 struct swallow_assign {
449 swallow_assign& operator=(const T&) {
453 } // namespace tuples
454 } // namespace detail
456 // "ignore" allows tuple positions to be ignored when using "tie".
458 detail::tuples::swallow_assign ignore;
461 // ---------------------------------------------------------------------------
462 // The call_traits for make_tuple
463 // Honours the reference_wrapper class.
465 // Must be instantiated with plain or const plain types (not with references)
467 // from template<class T> foo(const T& t) : make_tuple_traits<const T>::type
468 // from template<class T> foo(T& t) : make_tuple_traits<T>::type
472 // references -> compile_time_error
473 // reference_wrapper<T> -> T&
474 // const reference_wrapper<T> -> T&
475 // array -> const ref array
479 struct make_tuple_traits {
482 // commented away, see below (JJ)
483 // typedef typename IF<
484 // boost::is_function<T>::value,
490 // The is_function test was there originally for plain function types,
491 // which can't be stored as such (we must either store them as references or
492 // pointers). Such a type could be formed if make_tuple was called with a
493 // reference to a function.
494 // But this would mean that a const qualified function type was formed in
495 // the make_tuple function and hence make_tuple can't take a function
496 // reference as a parameter, and thus T can't be a function type.
497 // So is_function test was removed.
498 // (14.8.3. says that type deduction fails if a cv-qualified function type
499 // is created. (It only applies for the case of explicitly specifying template
500 // args, though?)) (JJ)
503 struct make_tuple_traits<T&> {
505 detail::tuples::generate_error<T&>::
506 do_not_use_with_reference_type error;
509 // Arrays can't be stored as plain types; convert them to references.
510 // All arrays are converted to const. This is because make_tuple takes its
511 // parameters as const T& and thus the knowledge of the potential
512 // non-constness of actual argument is lost.
513 template<class T, int n> struct make_tuple_traits <T[n]> {
514 typedef const T (&type)[n];
517 template<class T, int n>
518 struct make_tuple_traits<const T[n]> {
519 typedef const T (&type)[n];
522 template<class T, int n> struct make_tuple_traits<volatile T[n]> {
523 typedef const volatile T (&type)[n];
526 template<class T, int n>
527 struct make_tuple_traits<const volatile T[n]> {
528 typedef const volatile T (&type)[n];
532 struct make_tuple_traits<reference_wrapper<T> >{
537 struct make_tuple_traits<const reference_wrapper<T> >{
547 // a helper traits to make the make_tuple functions shorter (Vesa Karvonen's
550 class T0 = null_type, class T1 = null_type, class T2 = null_type,
551 class T3 = null_type, class T4 = null_type, class T5 = null_type,
552 class T6 = null_type, class T7 = null_type, class T8 = null_type,
555 struct make_tuple_mapper {
557 tuple<typename make_tuple_traits<T0>::type,
558 typename make_tuple_traits<T1>::type,
559 typename make_tuple_traits<T2>::type,
560 typename make_tuple_traits<T3>::type,
561 typename make_tuple_traits<T4>::type,
562 typename make_tuple_traits<T5>::type,
563 typename make_tuple_traits<T6>::type,
564 typename make_tuple_traits<T7>::type,
565 typename make_tuple_traits<T8>::type,
566 typename make_tuple_traits<T9>::type> type;
572 // -make_tuple function templates -----------------------------------
573 inline tuple<> make_tuple() {
578 inline typename boost::detail::tuples::make_tuple_mapper<T0>::type
579 make_tuple(const T0& t0) {
580 return typename boost::detail::tuples::make_tuple_mapper<T0>::type(t0);
583 template<class T0, class T1>
584 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1>::type
585 make_tuple(const T0& t0, const T1& t1) {
586 return typename boost::detail::tuples::make_tuple_mapper<T0, T1>::type(t0, t1);
589 template<class T0, class T1, class T2>
590 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2>::type
591 make_tuple(const T0& t0, const T1& t1, const T2& t2) {
592 return typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2>::type(t0, t1, t2);
595 template<class T0, class T1, class T2, class T3>
596 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3>::type
597 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3) {
598 return typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3>::type
602 template<class T0, class T1, class T2, class T3, class T4>
603 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4>::type
604 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
606 return typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4>::type
607 (t0, t1, t2, t3, t4);
610 template<class T0, class T1, class T2, class T3, class T4, class T5>
611 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4, T5>::type
612 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
613 const T4& t4, const T5& t5) {
614 return typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4, T5>::type
615 (t0, t1, t2, t3, t4, t5);
618 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6>
619 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6>::type
620 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
621 const T4& t4, const T5& t5, const T6& t6) {
622 return typename boost::detail::tuples::make_tuple_mapper
623 <T0, T1, T2, T3, T4, T5, T6>::type
624 (t0, t1, t2, t3, t4, t5, t6);
627 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6,
629 inline typename boost::detail::tuples::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6, T7>::type
630 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
631 const T4& t4, const T5& t5, const T6& t6, const T7& t7) {
632 return typename boost::detail::tuples::make_tuple_mapper
633 <T0, T1, T2, T3, T4, T5, T6, T7>::type
634 (t0, t1, t2, t3, t4, t5, t6, t7);
637 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6,
639 inline typename boost::detail::tuples::make_tuple_mapper
640 <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type
641 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
642 const T4& t4, const T5& t5, const T6& t6, const T7& t7,
644 return typename boost::detail::tuples::make_tuple_mapper
645 <T0, T1, T2, T3, T4, T5, T6, T7, T8>::type
646 (t0, t1, t2, t3, t4, t5, t6, t7, t8);
649 template<class T0, class T1, class T2, class T3, class T4, class T5, class T6,
650 class T7, class T8, class T9>
651 inline typename boost::detail::tuples::make_tuple_mapper
652 <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type
653 make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3,
654 const T4& t4, const T5& t5, const T6& t6, const T7& t7,
655 const T8& t8, const T9& t9) {
656 return typename boost::detail::tuples::make_tuple_mapper
657 <T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type
658 (t0, t1, t2, t3, t4, t5, t6, t7, t8, t9);
663 // Tie function templates -------------------------------------------------
665 inline tuple<T1&> tie(T1& t1) {
666 return tuple<T1&> (t1);
669 template<class T1, class T2>
670 inline tuple<T1&, T2&> tie(T1& t1, T2& t2) {
671 return tuple<T1&, T2&> (t1, t2);
674 template<class T1, class T2, class T3>
675 inline tuple<T1&, T2&, T3&> tie(T1& t1, T2& t2, T3& t3) {
676 return tuple<T1&, T2&, T3&> (t1, t2, t3);
679 template<class T1, class T2, class T3, class T4>
680 inline tuple<T1&, T2&, T3&, T4&> tie(T1& t1, T2& t2, T3& t3, T4& t4) {
681 return tuple<T1&, T2&, T3&, T4&> (t1, t2, t3, t4);
684 template<class T1, class T2, class T3, class T4, class T5>
685 inline tuple<T1&, T2&, T3&, T4&, T5&>
686 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5) {
687 return tuple<T1&, T2&, T3&, T4&, T5&> (t1, t2, t3, t4, t5);
690 template<class T1, class T2, class T3, class T4, class T5, class T6>
691 inline tuple<T1&, T2&, T3&, T4&, T5&, T6&>
692 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6) {
693 return tuple<T1&, T2&, T3&, T4&, T5&, T6&> (t1, t2, t3, t4, t5, t6);
696 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7>
697 inline tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&>
698 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7) {
699 return tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&> (t1, t2, t3, t4, t5, t6, t7);
702 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7,
704 inline tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&>
705 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8) {
706 return tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&>
707 (t1, t2, t3, t4, t5, t6, t7, t8);
710 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7,
712 inline tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&, T9&>
713 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8,
715 return tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&, T9&>
716 (t1, t2, t3, t4, t5, t6, t7, t8, t9);
719 template<class T1, class T2, class T3, class T4, class T5, class T6, class T7,
720 class T8, class T9, class T10>
721 inline tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&, T9&, T10&>
722 tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8,
724 return tuple<T1&, T2&, T3&, T4&, T5&, T6&, T7&, T8&, T9&, T10&>
725 (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10);
728 } // end of namespace boost
731 #endif // BOOST_TUPLE_BASIC_HPP