1 // operator_return_type_traits.hpp -- Boost Lambda Library ------------------
3 // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see www.boost.org
11 #ifndef BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP
12 #define BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP
14 #include "boost/lambda/detail/is_instance_of.hpp"
15 #include "boost/type_traits/is_same.hpp"
16 #include "boost/type_traits/is_pointer.hpp"
17 #include "boost/type_traits/is_float.hpp"
18 #include "boost/type_traits/is_convertible.hpp"
19 #include "boost/type_traits/remove_pointer.hpp"
20 #include "boost/type_traits/remove_const.hpp"
21 #include "boost/type_traits/remove_reference.hpp"
23 #include "boost/indirect_reference.hpp"
24 #include "boost/detail/container_fwd.hpp"
26 #include <cstddef> // needed for the ptrdiff_t
27 #include <iosfwd> // for istream and ostream
29 #include <iterator> // needed for operator&
35 // -- general helper templates for type deduction ------------------
37 // Much of the type deduction code for standard arithmetic types from Gary Powell
39 template <class A> struct promote_code { static const int value = -1; };
40 // this means that a code is not defined for A
42 // -- the next 5 types are needed in if_then_else_return
43 // the promotion order is not important, but they must have distinct values.
44 template <> struct promote_code<bool> { static const int value = 10; };
45 template <> struct promote_code<char> { static const int value = 20; };
46 template <> struct promote_code<unsigned char> { static const int value = 30; };
47 template <> struct promote_code<signed char> { static const int value = 40; };
48 template <> struct promote_code<short int> { static const int value = 50; };
51 template <> struct promote_code<int> { static const int value = 100; };
52 template <> struct promote_code<unsigned int> { static const int value = 200; };
53 template <> struct promote_code<long> { static const int value = 300; };
54 template <> struct promote_code<unsigned long> { static const int value = 400; };
56 template <> struct promote_code<float> { static const int value = 500; };
57 template <> struct promote_code<double> { static const int value = 600; };
58 template <> struct promote_code<long double> { static const int value = 700; };
62 // forward delcaration of complex.
72 template <> struct promote_code< std::complex<float> > { static const int value = 800; };
73 template <> struct promote_code< std::complex<double> > { static const int value = 900; };
74 template <> struct promote_code< std::complex<long double> > { static const int value = 1000; };
76 // -- int promotion -------------------------------------------
77 template <class T> struct promote_to_int { typedef T type; };
79 template <> struct promote_to_int<bool> { typedef int type; };
80 template <> struct promote_to_int<char> { typedef int type; };
81 template <> struct promote_to_int<unsigned char> { typedef int type; };
82 template <> struct promote_to_int<signed char> { typedef int type; };
83 template <> struct promote_to_int<short int> { typedef int type; };
85 // The unsigned short int promotion rule is this:
86 // unsigned short int to signed int if a signed int can hold all values
87 // of unsigned short int, otherwise go to unsigned int.
88 template <> struct promote_to_int<unsigned short int>
91 detail::IF<sizeof(int) <= sizeof(unsigned short int),
92 // I had the logic reversed but ">" messes up the parsing.
98 // TODO: think, should there be default behaviour for non-standard types?
100 } // namespace detail
102 // ------------------------------------------
103 // Unary actions ----------------------------
104 // ------------------------------------------
106 template<class Act, class A>
107 struct plain_return_type_1 {
108 typedef detail::unspecified type;
113 template<class Act, class A>
114 struct plain_return_type_1<unary_arithmetic_action<Act>, A> {
118 template<class Act, class A>
119 struct return_type_1<unary_arithmetic_action<Act>, A> {
121 typename plain_return_type_1<
122 unary_arithmetic_action<Act>,
123 typename detail::remove_reference_and_cv<A>::type
129 struct plain_return_type_1<bitwise_action<not_action>, A> {
133 // bitwise not, operator~()
134 template<class A> struct return_type_1<bitwise_action<not_action>, A> {
136 typename plain_return_type_1<
137 bitwise_action<not_action>,
138 typename detail::remove_reference_and_cv<A>::type
143 // prefix increment and decrement operators return
144 // their argument by default as a non-const reference
145 template<class Act, class A>
146 struct plain_return_type_1<pre_increment_decrement_action<Act>, A> {
150 template<class Act, class A>
151 struct return_type_1<pre_increment_decrement_action<Act>, A> {
153 typename plain_return_type_1<
154 pre_increment_decrement_action<Act>,
155 typename detail::remove_reference_and_cv<A>::type
159 // post decrement just returns the same plain type.
160 template<class Act, class A>
161 struct plain_return_type_1<post_increment_decrement_action<Act>, A> {
165 template<class Act, class A>
166 struct return_type_1<post_increment_decrement_action<Act>, A>
169 typename plain_return_type_1<
170 post_increment_decrement_action<Act>,
171 typename detail::remove_reference_and_cv<A>::type
175 // logical not, operator!()
177 struct plain_return_type_1<logical_action<not_action>, A> {
182 struct return_type_1<logical_action<not_action>, A> {
184 typename plain_return_type_1<
185 logical_action<not_action>,
186 typename detail::remove_reference_and_cv<A>::type
190 // address of action ---------------------------------------
194 struct return_type_1<other_action<addressof_action>, A> {
196 typename plain_return_type_1<
197 other_action<addressof_action>,
198 typename detail::remove_reference_and_cv<A>::type
201 // If no user defined specialization for A, then return the
202 // cv qualified pointer to A
203 typedef typename detail::IF<
204 boost::is_same<type1, detail::unspecified>::value,
205 typename boost::remove_reference<A>::type*,
210 // contentsof action ------------------------------------
212 // TODO: this deduction may lead to fail directly,
213 // (if A has no specialization for iterator_traits and has no
214 // typedef A::reference.
215 // There is no easy way around this, cause there doesn't seem to be a way
216 // to test whether a class is an iterator or not.
218 // The default works with std::iterators.
222 // A is a nonreference type
223 template <class A> struct contentsof_type {
224 typedef typename boost::indirect_reference<A>::type type;
227 // this is since the nullary () in lambda_functor is always instantiated
228 template <> struct contentsof_type<null_type> {
229 typedef detail::unspecified type;
233 template <class A> struct contentsof_type<const A> {
234 typedef typename contentsof_type<A>::type type;
237 template <class A> struct contentsof_type<volatile A> {
238 typedef typename contentsof_type<A>::type type;
241 template <class A> struct contentsof_type<const volatile A> {
242 typedef typename contentsof_type<A>::type type;
245 // standard iterator traits should take care of the pointer types
246 // but just to be on the safe side, we have the specializations here:
247 // these work even if A is cv-qualified.
248 template <class A> struct contentsof_type<A*> {
251 template <class A> struct contentsof_type<A* const> {
254 template <class A> struct contentsof_type<A* volatile> {
257 template <class A> struct contentsof_type<A* const volatile> {
261 template<class A, int N> struct contentsof_type<A[N]> {
264 template<class A, int N> struct contentsof_type<const A[N]> {
265 typedef const A& type;
267 template<class A, int N> struct contentsof_type<volatile A[N]> {
268 typedef volatile A& type;
270 template<class A, int N> struct contentsof_type<const volatile A[N]> {
271 typedef const volatile A& type;
281 struct return_type_1<other_action<contentsof_action>, A> {
284 typename plain_return_type_1<
285 other_action<contentsof_action>,
286 typename detail::remove_reference_and_cv<A>::type
289 // If no user defined specialization for A, then return the
290 // cv qualified pointer to A
293 boost::is_same<type1, detail::unspecified>::value,
294 detail::contentsof_type<
295 typename boost::remove_reference<A>::type
297 detail::identity_mapping<type1>
302 // ------------------------------------------------------------------
303 // binary actions ---------------------------------------------------
304 // ------------------------------------------------------------------
306 // here the default case is: no user defined versions:
307 template <class Act, class A, class B>
308 struct plain_return_type_2 {
309 typedef detail::unspecified type;
315 class illegal_pointer_arithmetic{};
317 // pointer arithmetic type deductions ----------------------
318 // value = false means that this is not a pointer arithmetic case
319 // value = true means, that this can be a pointer arithmetic case, but not necessarily is
320 // This means, that for user defined operators for pointer types, say for some operator+(X, *Y),
321 // the deductions must be coded at an earliel level (return_type_2).
323 template<class Act, class A, class B>
324 struct pointer_arithmetic_traits { static const bool value = false; };
326 template<class A, class B>
327 struct pointer_arithmetic_traits<plus_action, A, B> {
330 array_to_pointer<typename boost::remove_reference<A>::type>::type AP;
332 array_to_pointer<typename boost::remove_reference<B>::type>::type BP;
334 static const bool is_pointer_A = boost::is_pointer<AP>::value;
335 static const bool is_pointer_B = boost::is_pointer<BP>::value;
337 static const bool value = is_pointer_A || is_pointer_B;
339 // can't add two pointers.
340 // note, that we do not check wether the other type is valid for
341 // addition with a pointer.
342 // the compiler will catch it in the apply function
346 is_pointer_A && is_pointer_B,
347 detail::return_type_deduction_failure<
348 detail::illegal_pointer_arithmetic
350 typename detail::IF<is_pointer_A, AP, BP>::RET
355 template<class A, class B>
356 struct pointer_arithmetic_traits<minus_action, A, B> {
358 array_to_pointer<typename boost::remove_reference<A>::type>::type AP;
360 array_to_pointer<typename boost::remove_reference<B>::type>::type BP;
362 static const bool is_pointer_A = boost::is_pointer<AP>::value;
363 static const bool is_pointer_B = boost::is_pointer<BP>::value;
365 static const bool value = is_pointer_A || is_pointer_B;
367 static const bool same_pointer_type =
368 is_pointer_A && is_pointer_B &&
370 typename boost::remove_const<
371 typename boost::remove_pointer<
372 typename boost::remove_const<AP>::type
375 typename boost::remove_const<
376 typename boost::remove_pointer<
377 typename boost::remove_const<BP>::type
382 // ptr - ptr has type ptrdiff_t
383 // note, that we do not check if, in ptr - B, B is
384 // valid for subtraction with a pointer.
385 // the compiler will catch it in the apply function
389 same_pointer_type, const std::ptrdiff_t,
393 detail::return_type_deduction_failure<detail::illegal_pointer_arithmetic>
398 } // namespace detail
400 // -- arithmetic actions ---------------------------------------------
404 template<bool is_pointer_arithmetic, class Act, class A, class B>
405 struct return_type_2_arithmetic_phase_1;
407 template<class A, class B> struct return_type_2_arithmetic_phase_2;
408 template<class A, class B> struct return_type_2_arithmetic_phase_3;
410 } // namespace detail
413 // drop any qualifiers from the argument types within arithmetic_action
414 template<class A, class B, class Act>
415 struct return_type_2<arithmetic_action<Act>, A, B>
417 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
418 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
421 plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>::type type1;
423 // if user defined return type, do not enter the whole arithmetic deductions
426 boost::is_same<type1, detail::unspecified>::value,
427 detail::return_type_2_arithmetic_phase_1<
428 detail::pointer_arithmetic_traits<Act, A, B>::value, Act, A, B
430 plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>
436 // perform integral promotion, no pointer arithmetic
437 template<bool is_pointer_arithmetic, class Act, class A, class B>
438 struct return_type_2_arithmetic_phase_1
441 return_type_2_arithmetic_phase_2<
442 typename remove_reference_and_cv<A>::type,
443 typename remove_reference_and_cv<B>::type
447 // pointer_arithmetic
448 template<class Act, class A, class B>
449 struct return_type_2_arithmetic_phase_1<true, Act, A, B>
452 pointer_arithmetic_traits<Act, A, B>::type type;
455 template<class A, class B>
456 struct return_type_2_arithmetic_phase_2 {
458 return_type_2_arithmetic_phase_3<
459 typename promote_to_int<A>::type,
460 typename promote_to_int<B>::type
464 // specialization for unsigned int.
465 // We only have to do these two specialization because the value promotion will
466 // take care of the other cases.
467 // The unsigned int promotion rule is this:
468 // unsigned int to long if a long can hold all values of unsigned int,
469 // otherwise go to unsigned long.
471 // struct so I don't have to type this twice.
472 struct promotion_of_unsigned_int
475 detail::IF<sizeof(long) <= sizeof(unsigned int),
481 struct return_type_2_arithmetic_phase_2<unsigned int, long>
483 typedef promotion_of_unsigned_int::type type;
486 struct return_type_2_arithmetic_phase_2<long, unsigned int>
488 typedef promotion_of_unsigned_int::type type;
492 template<class A, class B> struct return_type_2_arithmetic_phase_3 {
493 enum { promote_code_A_value = promote_code<A>::value,
494 promote_code_B_value = promote_code<B>::value }; // enums for KCC
497 promote_code_A_value == -1 || promote_code_B_value == -1,
498 detail::return_type_deduction_failure<return_type_2_arithmetic_phase_3>,
500 ((int)promote_code_A_value > (int)promote_code_B_value),
507 } // namespace detail
509 // -- bitwise actions -------------------------------------------
510 // note: for integral types deuduction is similar to arithmetic actions.
512 // drop any qualifiers from the argument types within arithmetic action
513 template<class A, class B, class Act>
514 struct return_type_2<bitwise_action<Act>, A, B>
517 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
518 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
521 plain_return_type_2<bitwise_action<Act>, plain_A, plain_B>::type type1;
523 // if user defined return type, do not enter type deductions
526 boost::is_same<type1, detail::unspecified>::value,
527 return_type_2<arithmetic_action<plus_action>, A, B>,
528 plain_return_type_2<bitwise_action<Act>, plain_A, plain_B>
531 // plus_action is just a random pick, has to be a concrete instance
533 // TODO: This check is only valid for built-in types, overloaded types might
534 // accept floating point operators
536 // bitwise operators not defined for floating point types
537 // these test are not strictly needed here, since the error will be caught in
538 // the apply function
539 BOOST_STATIC_ASSERT(!(boost::is_float<plain_A>::value && boost::is_float<plain_B>::value));
546 template <class T> struct get_ostream_type {
547 typedef std::basic_ostream<typename T::char_type,
548 typename T::traits_type>& type;
551 template <class T> struct get_istream_type {
552 typedef std::basic_istream<typename T::char_type,
553 typename T::traits_type>& type;
556 template<class A, class B>
557 struct leftshift_type {
559 typedef typename boost::remove_reference<A>::type plainA;
561 typedef typename detail::IF_type<
562 is_instance_of_2<plainA, std::basic_ostream>::value,
563 get_ostream_type<plainA>, //reference to the stream
564 detail::remove_reference_and_cv<A>
568 template<class A, class B>
569 struct rightshift_type {
571 typedef typename boost::remove_reference<A>::type plainA;
573 typedef typename detail::IF_type<
574 is_instance_of_2<plainA, std::basic_istream>::value,
575 get_istream_type<plainA>, //reference to the stream
576 detail::remove_reference_and_cv<A>
585 template<class A, class B>
586 struct return_type_2<bitwise_action<leftshift_action>, A, B>
588 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
589 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
592 plain_return_type_2<bitwise_action<leftshift_action>, plain_A, plain_B>::type type1;
594 // if user defined return type, do not enter type deductions
597 boost::is_same<type1, detail::unspecified>::value,
598 detail::leftshift_type<A, B>,
599 plain_return_type_2<bitwise_action<leftshift_action>, plain_A, plain_B>
604 template<class A, class B>
605 struct return_type_2<bitwise_action<rightshift_action>, A, B>
607 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
608 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
611 plain_return_type_2<bitwise_action<rightshift_action>, plain_A, plain_B>::type type1;
613 // if user defined return type, do not enter type deductions
616 boost::is_same<type1, detail::unspecified>::value,
617 detail::rightshift_type<A, B>,
618 plain_return_type_2<bitwise_action<rightshift_action>, plain_A, plain_B>
622 // -- logical actions ----------------------------------------
624 // NOTE: this may not be true for some weird user-defined types,
625 template<class A, class B, class Act>
626 struct plain_return_type_2<logical_action<Act>, A, B> {
630 template<class A, class B, class Act>
631 struct return_type_2<logical_action<Act>, A, B> {
633 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
634 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
637 plain_return_type_2<logical_action<Act>, plain_A, plain_B>::type type;
642 // -- relational actions ----------------------------------------
644 // NOTE: this may not be true for some weird user-defined types,
645 template<class A, class B, class Act>
646 struct plain_return_type_2<relational_action<Act>, A, B> {
650 template<class A, class B, class Act>
651 struct return_type_2<relational_action<Act>, A, B> {
653 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
654 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
657 plain_return_type_2<relational_action<Act>, plain_A, plain_B>::type type;
660 // Assingment actions -----------------------------------------------
661 // return type is the type of the first argument as reference
663 // note that cv-qualifiers are preserved.
664 // Yes, assignment operator can be const!
666 // NOTE: this may not be true for some weird user-defined types,
668 template<class A, class B, class Act>
669 struct return_type_2<arithmetic_assignment_action<Act>, A, B> {
671 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
672 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
676 arithmetic_assignment_action<Act>, plain_A, plain_B
681 boost::is_same<type1, detail::unspecified>::value,
682 typename boost::add_reference<A>::type,
687 template<class A, class B, class Act>
688 struct return_type_2<bitwise_assignment_action<Act>, A, B> {
690 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
691 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
695 bitwise_assignment_action<Act>, plain_A, plain_B
700 boost::is_same<type1, detail::unspecified>::value,
701 typename boost::add_reference<A>::type,
706 template<class A, class B>
707 struct return_type_2<other_action<assignment_action>, A, B> {
708 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
709 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
713 other_action<assignment_action>, plain_A, plain_B
718 boost::is_same<type1, detail::unspecified>::value,
719 typename boost::add_reference<A>::type,
724 // -- other actions ----------------------------------------
726 // comma action ----------------------------------
727 // Note: this may not be true for some weird user-defined types,
729 // NOTE! This only tries the plain_return_type_2 layer and gives
730 // detail::unspecified as default. If no such specialization is found, the
731 // type rule in the spcecialization of the return_type_2_prot is used
732 // to give the type of the right argument (which can be a reference too)
733 // (The built in operator, can return a l- or rvalue).
734 template<class A, class B>
735 struct return_type_2<other_action<comma_action>, A, B> {
737 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
738 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
742 other_action<comma_action>, plain_A, plain_B
746 // subscript action -----------------------------------------------
750 // A and B are nonreference types
751 template <class A, class B> struct subscript_type {
752 typedef detail::unspecified type;
755 template <class A, class B> struct subscript_type<A*, B> {
758 template <class A, class B> struct subscript_type<A* const, B> {
761 template <class A, class B> struct subscript_type<A* volatile, B> {
764 template <class A, class B> struct subscript_type<A* const volatile, B> {
769 template<class A, class B, int N> struct subscript_type<A[N], B> {
773 // these 3 specializations are needed to make gcc <3 happy
774 template<class A, class B, int N> struct subscript_type<const A[N], B> {
775 typedef const A& type;
777 template<class A, class B, int N> struct subscript_type<volatile A[N], B> {
778 typedef volatile A& type;
780 template<class A, class B, int N> struct subscript_type<const volatile A[N], B> {
781 typedef const volatile A& type;
786 template<class A, class B>
787 struct return_type_2<other_action<subscript_action>, A, B> {
789 typedef typename detail::remove_reference_and_cv<A>::type plain_A;
790 typedef typename detail::remove_reference_and_cv<B>::type plain_B;
792 typedef typename boost::remove_reference<A>::type nonref_A;
793 typedef typename boost::remove_reference<B>::type nonref_B;
797 other_action<subscript_action>, plain_A, plain_B
802 boost::is_same<type1, detail::unspecified>::value,
803 detail::subscript_type<nonref_A, nonref_B>,
804 plain_return_type_2<other_action<subscript_action>, plain_A, plain_B>
809 template<class Key, class T, class Cmp, class Allocator, class B>
810 struct plain_return_type_2<other_action<subscript_action>, std::map<Key, T, Cmp, Allocator>, B> {
812 // T == std::map<Key, T, Cmp, Allocator>::mapped_type;
815 template<class Key, class T, class Cmp, class Allocator, class B>
816 struct plain_return_type_2<other_action<subscript_action>, std::multimap<Key, T, Cmp, Allocator>, B> {
818 // T == std::map<Key, T, Cmp, Allocator>::mapped_type;
822 template<class T, class Allocator, class B>
823 struct plain_return_type_2<other_action<subscript_action>, std::deque<T, Allocator>, B> {
824 typedef typename std::deque<T, Allocator>::reference type;
826 template<class T, class Allocator, class B>
827 struct plain_return_type_2<other_action<subscript_action>, const std::deque<T, Allocator>, B> {
828 typedef typename std::deque<T, Allocator>::const_reference type;
832 template<class T, class Allocator, class B>
833 struct plain_return_type_2<other_action<subscript_action>, std::vector<T, Allocator>, B> {
834 typedef typename std::vector<T, Allocator>::reference type;
836 template<class T, class Allocator, class B>
837 struct plain_return_type_2<other_action<subscript_action>, const std::vector<T, Allocator>, B> {
838 typedef typename std::vector<T, Allocator>::const_reference type;
842 template<class Char, class Traits, class Allocator, class B>
843 struct plain_return_type_2<other_action<subscript_action>, std::basic_string<Char, Traits, Allocator>, B> {
844 typedef typename std::basic_string<Char, Traits, Allocator>::reference type;
846 template<class Char, class Traits, class Allocator, class B>
847 struct plain_return_type_2<other_action<subscript_action>, const std::basic_string<Char, Traits, Allocator>, B> {
848 typedef typename std::basic_string<Char, Traits, Allocator>::const_reference type;
851 template<class Char, class Traits, class Allocator>
852 struct plain_return_type_2<arithmetic_action<plus_action>,
853 std::basic_string<Char, Traits, Allocator>,
854 std::basic_string<Char, Traits, Allocator> > {
855 typedef std::basic_string<Char, Traits, Allocator> type;
858 template<class Char, class Traits, class Allocator>
859 struct plain_return_type_2<arithmetic_action<plus_action>,
861 std::basic_string<Char, Traits, Allocator> > {
862 typedef std::basic_string<Char, Traits, Allocator> type;
865 template<class Char, class Traits, class Allocator>
866 struct plain_return_type_2<arithmetic_action<plus_action>,
867 std::basic_string<Char, Traits, Allocator>,
869 typedef std::basic_string<Char, Traits, Allocator> type;
872 template<class Char, class Traits, class Allocator, std::size_t N>
873 struct plain_return_type_2<arithmetic_action<plus_action>,
875 std::basic_string<Char, Traits, Allocator> > {
876 typedef std::basic_string<Char, Traits, Allocator> type;
879 template<class Char, class Traits, class Allocator, std::size_t N>
880 struct plain_return_type_2<arithmetic_action<plus_action>,
881 std::basic_string<Char, Traits, Allocator>,
883 typedef std::basic_string<Char, Traits, Allocator> type;
887 } // namespace lambda