operator_return_type_traits.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. // operator_return_type_traits.hpp -- Boost Lambda Library ------------------
  2. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see www.boost.org
  9. #ifndef BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP
  10. #define BOOST_LAMBDA_OPERATOR_RETURN_TYPE_TRAITS_HPP
  11. #include "boost/lambda/detail/is_instance_of.hpp"
  12. #include "boost/type_traits/is_same.hpp"
  13. #include "boost/type_traits/is_pointer.hpp"
  14. #include "boost/type_traits/is_float.hpp"
  15. #include "boost/type_traits/is_convertible.hpp"
  16. #include "boost/type_traits/remove_pointer.hpp"
  17. #include "boost/type_traits/remove_const.hpp"
  18. #include "boost/type_traits/remove_reference.hpp"
  19. #include "boost/indirect_reference.hpp"
  20. #include "boost/detail/container_fwd.hpp"
  21. #include <cstddef> // needed for the ptrdiff_t
  22. #include <iosfwd> // for istream and ostream
  23. #include <iterator> // needed for operator&
  24. namespace boost {
  25. namespace lambda {
  26. namespace detail {
  27. // -- general helper templates for type deduction ------------------
  28. // Much of the type deduction code for standard arithmetic types from Gary Powell
  29. template <class A> struct promote_code { static const int value = -1; };
  30. // this means that a code is not defined for A
  31. // -- the next 5 types are needed in if_then_else_return
  32. // the promotion order is not important, but they must have distinct values.
  33. template <> struct promote_code<bool> { static const int value = 10; };
  34. template <> struct promote_code<char> { static const int value = 20; };
  35. template <> struct promote_code<unsigned char> { static const int value = 30; };
  36. template <> struct promote_code<signed char> { static const int value = 40; };
  37. template <> struct promote_code<short int> { static const int value = 50; };
  38. // ----------
  39. template <> struct promote_code<int> { static const int value = 100; };
  40. template <> struct promote_code<unsigned int> { static const int value = 200; };
  41. template <> struct promote_code<long> { static const int value = 300; };
  42. template <> struct promote_code<unsigned long> { static const int value = 400; };
  43. template <> struct promote_code<float> { static const int value = 500; };
  44. template <> struct promote_code<double> { static const int value = 600; };
  45. template <> struct promote_code<long double> { static const int value = 700; };
  46. // TODO: wchar_t
  47. // forward delcaration of complex.
  48. } // namespace detail
  49. } // namespace lambda
  50. } // namespace boost
  51. namespace boost {
  52. namespace lambda {
  53. namespace detail {
  54. template <> struct promote_code< std::complex<float> > { static const int value = 800; };
  55. template <> struct promote_code< std::complex<double> > { static const int value = 900; };
  56. template <> struct promote_code< std::complex<long double> > { static const int value = 1000; };
  57. // -- int promotion -------------------------------------------
  58. template <class T> struct promote_to_int { typedef T type; };
  59. template <> struct promote_to_int<bool> { typedef int type; };
  60. template <> struct promote_to_int<char> { typedef int type; };
  61. template <> struct promote_to_int<unsigned char> { typedef int type; };
  62. template <> struct promote_to_int<signed char> { typedef int type; };
  63. template <> struct promote_to_int<short int> { typedef int type; };
  64. // The unsigned short int promotion rule is this:
  65. // unsigned short int to signed int if a signed int can hold all values
  66. // of unsigned short int, otherwise go to unsigned int.
  67. template <> struct promote_to_int<unsigned short int>
  68. {
  69. typedef
  70. detail::IF<sizeof(int) <= sizeof(unsigned short int),
  71. // I had the logic reversed but ">" messes up the parsing.
  72. unsigned int,
  73. int>::RET type;
  74. };
  75. // TODO: think, should there be default behaviour for non-standard types?
  76. } // namespace detail
  77. // ------------------------------------------
  78. // Unary actions ----------------------------
  79. // ------------------------------------------
  80. template<class Act, class A>
  81. struct plain_return_type_1 {
  82. typedef detail::unspecified type;
  83. };
  84. template<class Act, class A>
  85. struct plain_return_type_1<unary_arithmetic_action<Act>, A> {
  86. typedef A type;
  87. };
  88. template<class Act, class A>
  89. struct return_type_1<unary_arithmetic_action<Act>, A> {
  90. typedef
  91. typename plain_return_type_1<
  92. unary_arithmetic_action<Act>,
  93. typename detail::remove_reference_and_cv<A>::type
  94. >::type type;
  95. };
  96. template<class A>
  97. struct plain_return_type_1<bitwise_action<not_action>, A> {
  98. typedef A type;
  99. };
  100. // bitwise not, operator~()
  101. template<class A> struct return_type_1<bitwise_action<not_action>, A> {
  102. typedef
  103. typename plain_return_type_1<
  104. bitwise_action<not_action>,
  105. typename detail::remove_reference_and_cv<A>::type
  106. >::type type;
  107. };
  108. // prefix increment and decrement operators return
  109. // their argument by default as a non-const reference
  110. template<class Act, class A>
  111. struct plain_return_type_1<pre_increment_decrement_action<Act>, A> {
  112. typedef A& type;
  113. };
  114. template<class Act, class A>
  115. struct return_type_1<pre_increment_decrement_action<Act>, A> {
  116. typedef
  117. typename plain_return_type_1<
  118. pre_increment_decrement_action<Act>,
  119. typename detail::remove_reference_and_cv<A>::type
  120. >::type type;
  121. };
  122. // post decrement just returns the same plain type.
  123. template<class Act, class A>
  124. struct plain_return_type_1<post_increment_decrement_action<Act>, A> {
  125. typedef A type;
  126. };
  127. template<class Act, class A>
  128. struct return_type_1<post_increment_decrement_action<Act>, A>
  129. {
  130. typedef
  131. typename plain_return_type_1<
  132. post_increment_decrement_action<Act>,
  133. typename detail::remove_reference_and_cv<A>::type
  134. >::type type;
  135. };
  136. // logical not, operator!()
  137. template<class A>
  138. struct plain_return_type_1<logical_action<not_action>, A> {
  139. typedef bool type;
  140. };
  141. template<class A>
  142. struct return_type_1<logical_action<not_action>, A> {
  143. typedef
  144. typename plain_return_type_1<
  145. logical_action<not_action>,
  146. typename detail::remove_reference_and_cv<A>::type
  147. >::type type;
  148. };
  149. // address of action ---------------------------------------
  150. template<class A>
  151. struct return_type_1<other_action<addressof_action>, A> {
  152. typedef
  153. typename plain_return_type_1<
  154. other_action<addressof_action>,
  155. typename detail::remove_reference_and_cv<A>::type
  156. >::type type1;
  157. // If no user defined specialization for A, then return the
  158. // cv qualified pointer to A
  159. typedef typename detail::IF<
  160. boost::is_same<type1, detail::unspecified>::value,
  161. typename boost::remove_reference<A>::type*,
  162. type1
  163. >::RET type;
  164. };
  165. // contentsof action ------------------------------------
  166. // TODO: this deduction may lead to fail directly,
  167. // (if A has no specialization for iterator_traits and has no
  168. // typedef A::reference.
  169. // There is no easy way around this, cause there doesn't seem to be a way
  170. // to test whether a class is an iterator or not.
  171. // The default works with std::iterators.
  172. namespace detail {
  173. // A is a nonreference type
  174. template <class A> struct contentsof_type {
  175. typedef typename boost::indirect_reference<A>::type type;
  176. };
  177. // this is since the nullary () in lambda_functor is always instantiated
  178. template <> struct contentsof_type<null_type> {
  179. typedef detail::unspecified type;
  180. };
  181. template <class A> struct contentsof_type<const A> {
  182. typedef typename contentsof_type<A>::type type;
  183. };
  184. template <class A> struct contentsof_type<volatile A> {
  185. typedef typename contentsof_type<A>::type type;
  186. };
  187. template <class A> struct contentsof_type<const volatile A> {
  188. typedef typename contentsof_type<A>::type type;
  189. };
  190. // standard iterator traits should take care of the pointer types
  191. // but just to be on the safe side, we have the specializations here:
  192. // these work even if A is cv-qualified.
  193. template <class A> struct contentsof_type<A*> {
  194. typedef A& type;
  195. };
  196. template <class A> struct contentsof_type<A* const> {
  197. typedef A& type;
  198. };
  199. template <class A> struct contentsof_type<A* volatile> {
  200. typedef A& type;
  201. };
  202. template <class A> struct contentsof_type<A* const volatile> {
  203. typedef A& type;
  204. };
  205. template<class A, int N> struct contentsof_type<A[N]> {
  206. typedef A& type;
  207. };
  208. template<class A, int N> struct contentsof_type<const A[N]> {
  209. typedef const A& type;
  210. };
  211. template<class A, int N> struct contentsof_type<volatile A[N]> {
  212. typedef volatile A& type;
  213. };
  214. template<class A, int N> struct contentsof_type<const volatile A[N]> {
  215. typedef const volatile A& type;
  216. };
  217. } // end detail
  218. template<class A>
  219. struct return_type_1<other_action<contentsof_action>, A> {
  220. typedef
  221. typename plain_return_type_1<
  222. other_action<contentsof_action>,
  223. typename detail::remove_reference_and_cv<A>::type
  224. >::type type1;
  225. // If no user defined specialization for A, then return the
  226. // cv qualified pointer to A
  227. typedef typename
  228. detail::IF_type<
  229. boost::is_same<type1, detail::unspecified>::value,
  230. detail::contentsof_type<
  231. typename boost::remove_reference<A>::type
  232. >,
  233. detail::identity_mapping<type1>
  234. >::type type;
  235. };
  236. // ------------------------------------------------------------------
  237. // binary actions ---------------------------------------------------
  238. // ------------------------------------------------------------------
  239. // here the default case is: no user defined versions:
  240. template <class Act, class A, class B>
  241. struct plain_return_type_2 {
  242. typedef detail::unspecified type;
  243. };
  244. namespace detail {
  245. // error classes
  246. class illegal_pointer_arithmetic{};
  247. // pointer arithmetic type deductions ----------------------
  248. // value = false means that this is not a pointer arithmetic case
  249. // value = true means, that this can be a pointer arithmetic case, but not necessarily is
  250. // This means, that for user defined operators for pointer types, say for some operator+(X, *Y),
  251. // the deductions must be coded at an earliel level (return_type_2).
  252. template<class Act, class A, class B>
  253. struct pointer_arithmetic_traits { static const bool value = false; };
  254. template<class A, class B>
  255. struct pointer_arithmetic_traits<plus_action, A, B> {
  256. typedef typename
  257. array_to_pointer<typename boost::remove_reference<A>::type>::type AP;
  258. typedef typename
  259. array_to_pointer<typename boost::remove_reference<B>::type>::type BP;
  260. static const bool is_pointer_A = boost::is_pointer<AP>::value;
  261. static const bool is_pointer_B = boost::is_pointer<BP>::value;
  262. static const bool value = is_pointer_A || is_pointer_B;
  263. // can't add two pointers.
  264. // note, that we do not check wether the other type is valid for
  265. // addition with a pointer.
  266. // the compiler will catch it in the apply function
  267. typedef typename
  268. detail::IF<
  269. is_pointer_A && is_pointer_B,
  270. detail::return_type_deduction_failure<
  271. detail::illegal_pointer_arithmetic
  272. >,
  273. typename detail::IF<is_pointer_A, AP, BP>::RET
  274. >::RET type;
  275. };
  276. template<class A, class B>
  277. struct pointer_arithmetic_traits<minus_action, A, B> {
  278. typedef typename
  279. array_to_pointer<typename boost::remove_reference<A>::type>::type AP;
  280. typedef typename
  281. array_to_pointer<typename boost::remove_reference<B>::type>::type BP;
  282. static const bool is_pointer_A = boost::is_pointer<AP>::value;
  283. static const bool is_pointer_B = boost::is_pointer<BP>::value;
  284. static const bool value = is_pointer_A || is_pointer_B;
  285. static const bool same_pointer_type =
  286. is_pointer_A && is_pointer_B &&
  287. boost::is_same<
  288. typename boost::remove_const<
  289. typename boost::remove_pointer<
  290. typename boost::remove_const<AP>::type
  291. >::type
  292. >::type,
  293. typename boost::remove_const<
  294. typename boost::remove_pointer<
  295. typename boost::remove_const<BP>::type
  296. >::type
  297. >::type
  298. >::value;
  299. // ptr - ptr has type ptrdiff_t
  300. // note, that we do not check if, in ptr - B, B is
  301. // valid for subtraction with a pointer.
  302. // the compiler will catch it in the apply function
  303. typedef typename
  304. detail::IF<
  305. same_pointer_type, const std::ptrdiff_t,
  306. typename detail::IF<
  307. is_pointer_A,
  308. AP,
  309. detail::return_type_deduction_failure<detail::illegal_pointer_arithmetic>
  310. >::RET
  311. >::RET type;
  312. };
  313. } // namespace detail
  314. // -- arithmetic actions ---------------------------------------------
  315. namespace detail {
  316. template<bool is_pointer_arithmetic, class Act, class A, class B>
  317. struct return_type_2_arithmetic_phase_1;
  318. template<class A, class B> struct return_type_2_arithmetic_phase_2;
  319. template<class A, class B> struct return_type_2_arithmetic_phase_3;
  320. } // namespace detail
  321. // drop any qualifiers from the argument types within arithmetic_action
  322. template<class A, class B, class Act>
  323. struct return_type_2<arithmetic_action<Act>, A, B>
  324. {
  325. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  326. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  327. typedef typename
  328. plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>::type type1;
  329. // if user defined return type, do not enter the whole arithmetic deductions
  330. typedef typename
  331. detail::IF_type<
  332. boost::is_same<type1, detail::unspecified>::value,
  333. detail::return_type_2_arithmetic_phase_1<
  334. detail::pointer_arithmetic_traits<Act, A, B>::value, Act, A, B
  335. >,
  336. plain_return_type_2<arithmetic_action<Act>, plain_A, plain_B>
  337. >::type type;
  338. };
  339. namespace detail {
  340. // perform integral promotion, no pointer arithmetic
  341. template<bool is_pointer_arithmetic, class Act, class A, class B>
  342. struct return_type_2_arithmetic_phase_1
  343. {
  344. typedef typename
  345. return_type_2_arithmetic_phase_2<
  346. typename remove_reference_and_cv<A>::type,
  347. typename remove_reference_and_cv<B>::type
  348. >::type type;
  349. };
  350. // pointer_arithmetic
  351. template<class Act, class A, class B>
  352. struct return_type_2_arithmetic_phase_1<true, Act, A, B>
  353. {
  354. typedef typename
  355. pointer_arithmetic_traits<Act, A, B>::type type;
  356. };
  357. template<class A, class B>
  358. struct return_type_2_arithmetic_phase_2 {
  359. typedef typename
  360. return_type_2_arithmetic_phase_3<
  361. typename promote_to_int<A>::type,
  362. typename promote_to_int<B>::type
  363. >::type type;
  364. };
  365. // specialization for unsigned int.
  366. // We only have to do these two specialization because the value promotion will
  367. // take care of the other cases.
  368. // The unsigned int promotion rule is this:
  369. // unsigned int to long if a long can hold all values of unsigned int,
  370. // otherwise go to unsigned long.
  371. // struct so I don't have to type this twice.
  372. struct promotion_of_unsigned_int
  373. {
  374. typedef
  375. detail::IF<sizeof(long) <= sizeof(unsigned int),
  376. unsigned long,
  377. long>::RET type;
  378. };
  379. template<>
  380. struct return_type_2_arithmetic_phase_2<unsigned int, long>
  381. {
  382. typedef promotion_of_unsigned_int::type type;
  383. };
  384. template<>
  385. struct return_type_2_arithmetic_phase_2<long, unsigned int>
  386. {
  387. typedef promotion_of_unsigned_int::type type;
  388. };
  389. template<class A, class B> struct return_type_2_arithmetic_phase_3 {
  390. enum { promote_code_A_value = promote_code<A>::value,
  391. promote_code_B_value = promote_code<B>::value }; // enums for KCC
  392. typedef typename
  393. detail::IF<
  394. promote_code_A_value == -1 || promote_code_B_value == -1,
  395. detail::return_type_deduction_failure<return_type_2_arithmetic_phase_3>,
  396. typename detail::IF<
  397. ((int)promote_code_A_value > (int)promote_code_B_value),
  398. A,
  399. B
  400. >::RET
  401. >::RET type;
  402. };
  403. } // namespace detail
  404. // -- bitwise actions -------------------------------------------
  405. // note: for integral types deuduction is similar to arithmetic actions.
  406. // drop any qualifiers from the argument types within arithmetic action
  407. template<class A, class B, class Act>
  408. struct return_type_2<bitwise_action<Act>, A, B>
  409. {
  410. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  411. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  412. typedef typename
  413. plain_return_type_2<bitwise_action<Act>, plain_A, plain_B>::type type1;
  414. // if user defined return type, do not enter type deductions
  415. typedef typename
  416. detail::IF_type<
  417. boost::is_same<type1, detail::unspecified>::value,
  418. return_type_2<arithmetic_action<plus_action>, A, B>,
  419. plain_return_type_2<bitwise_action<Act>, plain_A, plain_B>
  420. >::type type;
  421. // plus_action is just a random pick, has to be a concrete instance
  422. // TODO: This check is only valid for built-in types, overloaded types might
  423. // accept floating point operators
  424. // bitwise operators not defined for floating point types
  425. // these test are not strictly needed here, since the error will be caught in
  426. // the apply function
  427. BOOST_STATIC_ASSERT(!(boost::is_float<plain_A>::value && boost::is_float<plain_B>::value));
  428. };
  429. namespace detail {
  430. template <class T> struct get_ostream_type {
  431. typedef std::basic_ostream<typename T::char_type,
  432. typename T::traits_type>& type;
  433. };
  434. template <class T> struct get_istream_type {
  435. typedef std::basic_istream<typename T::char_type,
  436. typename T::traits_type>& type;
  437. };
  438. template<class A, class B>
  439. struct leftshift_type {
  440. private:
  441. typedef typename boost::remove_reference<A>::type plainA;
  442. public:
  443. typedef typename detail::IF_type<
  444. is_instance_of_2<plainA, std::basic_ostream>::value,
  445. get_ostream_type<plainA>, //reference to the stream
  446. detail::remove_reference_and_cv<A>
  447. >::type type;
  448. };
  449. template<class A, class B>
  450. struct rightshift_type {
  451. private:
  452. typedef typename boost::remove_reference<A>::type plainA;
  453. public:
  454. typedef typename detail::IF_type<
  455. is_instance_of_2<plainA, std::basic_istream>::value,
  456. get_istream_type<plainA>, //reference to the stream
  457. detail::remove_reference_and_cv<A>
  458. >::type type;
  459. };
  460. } // end detail
  461. // ostream
  462. template<class A, class B>
  463. struct return_type_2<bitwise_action<leftshift_action>, A, B>
  464. {
  465. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  466. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  467. typedef typename
  468. plain_return_type_2<bitwise_action<leftshift_action>, plain_A, plain_B>::type type1;
  469. // if user defined return type, do not enter type deductions
  470. typedef typename
  471. detail::IF_type<
  472. boost::is_same<type1, detail::unspecified>::value,
  473. detail::leftshift_type<A, B>,
  474. plain_return_type_2<bitwise_action<leftshift_action>, plain_A, plain_B>
  475. >::type type;
  476. };
  477. // istream
  478. template<class A, class B>
  479. struct return_type_2<bitwise_action<rightshift_action>, A, B>
  480. {
  481. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  482. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  483. typedef typename
  484. plain_return_type_2<bitwise_action<rightshift_action>, plain_A, plain_B>::type type1;
  485. // if user defined return type, do not enter type deductions
  486. typedef typename
  487. detail::IF_type<
  488. boost::is_same<type1, detail::unspecified>::value,
  489. detail::rightshift_type<A, B>,
  490. plain_return_type_2<bitwise_action<rightshift_action>, plain_A, plain_B>
  491. >::type type;
  492. };
  493. // -- logical actions ----------------------------------------
  494. // always bool
  495. // NOTE: this may not be true for some weird user-defined types,
  496. template<class A, class B, class Act>
  497. struct plain_return_type_2<logical_action<Act>, A, B> {
  498. typedef bool type;
  499. };
  500. template<class A, class B, class Act>
  501. struct return_type_2<logical_action<Act>, A, B> {
  502. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  503. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  504. typedef typename
  505. plain_return_type_2<logical_action<Act>, plain_A, plain_B>::type type;
  506. };
  507. // -- relational actions ----------------------------------------
  508. // always bool
  509. // NOTE: this may not be true for some weird user-defined types,
  510. template<class A, class B, class Act>
  511. struct plain_return_type_2<relational_action<Act>, A, B> {
  512. typedef bool type;
  513. };
  514. template<class A, class B, class Act>
  515. struct return_type_2<relational_action<Act>, A, B> {
  516. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  517. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  518. typedef typename
  519. plain_return_type_2<relational_action<Act>, plain_A, plain_B>::type type;
  520. };
  521. // Assingment actions -----------------------------------------------
  522. // return type is the type of the first argument as reference
  523. // note that cv-qualifiers are preserved.
  524. // Yes, assignment operator can be const!
  525. // NOTE: this may not be true for some weird user-defined types,
  526. template<class A, class B, class Act>
  527. struct return_type_2<arithmetic_assignment_action<Act>, A, B> {
  528. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  529. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  530. typedef typename
  531. plain_return_type_2<
  532. arithmetic_assignment_action<Act>, plain_A, plain_B
  533. >::type type1;
  534. typedef typename
  535. detail::IF<
  536. boost::is_same<type1, detail::unspecified>::value,
  537. typename boost::add_reference<A>::type,
  538. type1
  539. >::RET type;
  540. };
  541. template<class A, class B, class Act>
  542. struct return_type_2<bitwise_assignment_action<Act>, A, B> {
  543. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  544. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  545. typedef typename
  546. plain_return_type_2<
  547. bitwise_assignment_action<Act>, plain_A, plain_B
  548. >::type type1;
  549. typedef typename
  550. detail::IF<
  551. boost::is_same<type1, detail::unspecified>::value,
  552. typename boost::add_reference<A>::type,
  553. type1
  554. >::RET type;
  555. };
  556. template<class A, class B>
  557. struct return_type_2<other_action<assignment_action>, A, B> {
  558. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  559. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  560. typedef typename
  561. plain_return_type_2<
  562. other_action<assignment_action>, plain_A, plain_B
  563. >::type type1;
  564. typedef typename
  565. detail::IF<
  566. boost::is_same<type1, detail::unspecified>::value,
  567. typename boost::add_reference<A>::type,
  568. type1
  569. >::RET type;
  570. };
  571. // -- other actions ----------------------------------------
  572. // comma action ----------------------------------
  573. // Note: this may not be true for some weird user-defined types,
  574. // NOTE! This only tries the plain_return_type_2 layer and gives
  575. // detail::unspecified as default. If no such specialization is found, the
  576. // type rule in the spcecialization of the return_type_2_prot is used
  577. // to give the type of the right argument (which can be a reference too)
  578. // (The built in operator, can return a l- or rvalue).
  579. template<class A, class B>
  580. struct return_type_2<other_action<comma_action>, A, B> {
  581. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  582. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  583. typedef typename
  584. plain_return_type_2<
  585. other_action<comma_action>, plain_A, plain_B
  586. >::type type;
  587. };
  588. // subscript action -----------------------------------------------
  589. namespace detail {
  590. // A and B are nonreference types
  591. template <class A, class B> struct subscript_type {
  592. typedef detail::unspecified type;
  593. };
  594. template <class A, class B> struct subscript_type<A*, B> {
  595. typedef A& type;
  596. };
  597. template <class A, class B> struct subscript_type<A* const, B> {
  598. typedef A& type;
  599. };
  600. template <class A, class B> struct subscript_type<A* volatile, B> {
  601. typedef A& type;
  602. };
  603. template <class A, class B> struct subscript_type<A* const volatile, B> {
  604. typedef A& type;
  605. };
  606. template<class A, class B, int N> struct subscript_type<A[N], B> {
  607. typedef A& type;
  608. };
  609. // these 3 specializations are needed to make gcc <3 happy
  610. template<class A, class B, int N> struct subscript_type<const A[N], B> {
  611. typedef const A& type;
  612. };
  613. template<class A, class B, int N> struct subscript_type<volatile A[N], B> {
  614. typedef volatile A& type;
  615. };
  616. template<class A, class B, int N> struct subscript_type<const volatile A[N], B> {
  617. typedef const volatile A& type;
  618. };
  619. } // end detail
  620. template<class A, class B>
  621. struct return_type_2<other_action<subscript_action>, A, B> {
  622. typedef typename detail::remove_reference_and_cv<A>::type plain_A;
  623. typedef typename detail::remove_reference_and_cv<B>::type plain_B;
  624. typedef typename boost::remove_reference<A>::type nonref_A;
  625. typedef typename boost::remove_reference<B>::type nonref_B;
  626. typedef typename
  627. plain_return_type_2<
  628. other_action<subscript_action>, plain_A, plain_B
  629. >::type type1;
  630. typedef typename
  631. detail::IF_type<
  632. boost::is_same<type1, detail::unspecified>::value,
  633. detail::subscript_type<nonref_A, nonref_B>,
  634. plain_return_type_2<other_action<subscript_action>, plain_A, plain_B>
  635. >::type type;
  636. };
  637. template<class Key, class T, class Cmp, class Allocator, class B>
  638. struct plain_return_type_2<other_action<subscript_action>, std::map<Key, T, Cmp, Allocator>, B> {
  639. typedef T& type;
  640. // T == std::map<Key, T, Cmp, Allocator>::mapped_type;
  641. };
  642. template<class Key, class T, class Cmp, class Allocator, class B>
  643. struct plain_return_type_2<other_action<subscript_action>, std::multimap<Key, T, Cmp, Allocator>, B> {
  644. typedef T& type;
  645. // T == std::map<Key, T, Cmp, Allocator>::mapped_type;
  646. };
  647. // deque
  648. template<class T, class Allocator, class B>
  649. struct plain_return_type_2<other_action<subscript_action>, std::deque<T, Allocator>, B> {
  650. typedef typename std::deque<T, Allocator>::reference type;
  651. };
  652. template<class T, class Allocator, class B>
  653. struct plain_return_type_2<other_action<subscript_action>, const std::deque<T, Allocator>, B> {
  654. typedef typename std::deque<T, Allocator>::const_reference type;
  655. };
  656. // vector
  657. template<class T, class Allocator, class B>
  658. struct plain_return_type_2<other_action<subscript_action>, std::vector<T, Allocator>, B> {
  659. typedef typename std::vector<T, Allocator>::reference type;
  660. };
  661. template<class T, class Allocator, class B>
  662. struct plain_return_type_2<other_action<subscript_action>, const std::vector<T, Allocator>, B> {
  663. typedef typename std::vector<T, Allocator>::const_reference type;
  664. };
  665. // basic_string
  666. template<class Char, class Traits, class Allocator, class B>
  667. struct plain_return_type_2<other_action<subscript_action>, std::basic_string<Char, Traits, Allocator>, B> {
  668. typedef typename std::basic_string<Char, Traits, Allocator>::reference type;
  669. };
  670. template<class Char, class Traits, class Allocator, class B>
  671. struct plain_return_type_2<other_action<subscript_action>, const std::basic_string<Char, Traits, Allocator>, B> {
  672. typedef typename std::basic_string<Char, Traits, Allocator>::const_reference type;
  673. };
  674. template<class Char, class Traits, class Allocator>
  675. struct plain_return_type_2<arithmetic_action<plus_action>,
  676. std::basic_string<Char, Traits, Allocator>,
  677. std::basic_string<Char, Traits, Allocator> > {
  678. typedef std::basic_string<Char, Traits, Allocator> type;
  679. };
  680. template<class Char, class Traits, class Allocator>
  681. struct plain_return_type_2<arithmetic_action<plus_action>,
  682. const Char*,
  683. std::basic_string<Char, Traits, Allocator> > {
  684. typedef std::basic_string<Char, Traits, Allocator> type;
  685. };
  686. template<class Char, class Traits, class Allocator>
  687. struct plain_return_type_2<arithmetic_action<plus_action>,
  688. std::basic_string<Char, Traits, Allocator>,
  689. const Char*> {
  690. typedef std::basic_string<Char, Traits, Allocator> type;
  691. };
  692. template<class Char, class Traits, class Allocator, std::size_t N>
  693. struct plain_return_type_2<arithmetic_action<plus_action>,
  694. Char[N],
  695. std::basic_string<Char, Traits, Allocator> > {
  696. typedef std::basic_string<Char, Traits, Allocator> type;
  697. };
  698. template<class Char, class Traits, class Allocator, std::size_t N>
  699. struct plain_return_type_2<arithmetic_action<plus_action>,
  700. std::basic_string<Char, Traits, Allocator>,
  701. Char[N]> {
  702. typedef std::basic_string<Char, Traits, Allocator> type;
  703. };
  704. } // namespace lambda
  705. } // namespace boost
  706. #endif