interoperability-revisited.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. ++++++++++++++++++++++++++++
  2. Interoperability Revisited
  3. ++++++++++++++++++++++++++++
  4. :date: $Date$
  5. :copyright: Copyright Thomas Witt 2004.
  6. .. Distributed under the Boost
  7. .. Software License, Version 1.0. (See accompanying
  8. .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. Problem
  10. =======
  11. The current iterator_facade specification makes it unneccessarily tedious to
  12. implement interoperable iterators.
  13. In the following text a simplified example of the current iterator_facade specification is used to
  14. illustrate the problem.
  15. In the current specification binary operators are implemented in the following way::
  16. template <class Derived>
  17. struct Facade
  18. {
  19. };
  20. template <class T1, T2>
  21. struct is_interoperable :
  22. or_<
  23. is_convertible<T1, T2>
  24. , is_convertible<T2, T1>
  25. >
  26. {};
  27. template<
  28. class Derived1
  29. , class Derived2
  30. >
  31. enable_if<is_interoperable<Derived1, Derived2>, bool> operator==(
  32. Derived1 const& lhs
  33. , Derived2 const& rhs
  34. )
  35. {
  36. return static_cast<Derived1 const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
  37. }
  38. The problem with this is that operator== always forwards to Derived1::equal_to. The net effect is that the
  39. following "obvious" implementation of to interoperable types does
  40. not quite work. ::
  41. struct Mutable : Facade<Mutable>
  42. {
  43. bool equal_to(Mutable const&);
  44. };
  45. struct Constant : Facade<Constant>
  46. {
  47. Constant();
  48. Constant(Constant const&);
  49. Constant(Mutable const&);
  50. ...
  51. bool equal_to(Constant const&);
  52. };
  53. Constant c;
  54. Mutable m;
  55. c == m; // ok, dispatched to Constant::equal_to
  56. m == c; // !! error, dispatched to Mutable::equal_to
  57. Instead the following "slightly" more complicated implementation is necessary
  58. struct Mutable : Facade<Mutable>
  59. {
  60. template <class T>
  61. enable_if<is_convertible<Mutable, T> || is_convertible<T, Mutable>, bool>::type equal_to(T const&);
  62. };
  63. struct Constant : Tag<Constant>
  64. {
  65. Constant();
  66. Constant(Constant const&);
  67. Constant(Mutable const&);
  68. template <class T>
  69. enable_if<is_convertible<Constant, T> || is_convertible<T, Constant>, bool>::type equal_to(T const&);
  70. };
  71. Beside the fact that the code is significantly more complex to understand and to teach there is
  72. a major design problem lurking here. Note that in both types equal_to is a function template with
  73. an unconstrained argument T. This is necessary so that further types can be made interoperable with
  74. Mutable or Constant. Would Mutable be defined as ::
  75. struct Mutable : Facade<Mutable>
  76. {
  77. bool equal_to(Mutable const&);
  78. bool equal_to(Constant const&);
  79. };
  80. Constant and Mutable would still be interoperable but no further interoperable could be added
  81. without changing Mutable. Even if this would be considered acceptable the current specification forces
  82. a two way dependency between interoperable types. Note in the templated equal_to case this dependency
  83. is implicitly created when specializing equal_to.
  84. Solution
  85. ========
  86. The two way dependency can be avoided by enabling type conversion in the binary operator
  87. implementation. Note that this is the usual way interoperability betwween types is achieved
  88. for binary operators and one reason why binary operators are usually implemented as non-members.
  89. A simple implementation of this strategy would look like this ::
  90. template<
  91. class T1
  92. , class T2
  93. >
  94. struct interoperable_base :
  95. if_<
  96. is_convertible<
  97. T2
  98. , T1
  99. >
  100. , T1
  101. , T2>
  102. {};
  103. template<
  104. class Derived1
  105. , class Derived2
  106. >
  107. enable_if<is_interoperable<Derived1, Derived2>, bool> operator==(
  108. Derived1 const& lhs
  109. , Derived2 const& rhs
  110. )
  111. {
  112. typedef interoperable_base<
  113. Derived1
  114. , Derived2
  115. >::type Base;
  116. return static_cast<Base const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
  117. }
  118. This way our original simple and "obvious" implementation would
  119. work again. ::
  120. c == m; // ok, dispatched to Constant::equal_to
  121. m == c; // ok, dispatched to Constant::equal_to, m converted to Constant
  122. The backdraw of this approach is that a possibly costly conversion of iterator objects
  123. is forced on the user even in cases where direct comparison could be implemented
  124. in a much more efficient way. This problem arises especially for iterator_adaptor
  125. specializations and can be significantly slow down the iteration over ranges. Given the fact
  126. that iteration is a very basic operation this possible performance degradation is not
  127. acceptable.
  128. Luckily whe can have our cake and eat it by a slightly more clever implementation of the binary
  129. operators. ::
  130. template<
  131. class Derived1
  132. , class Derived2
  133. >
  134. enable_if<is_convertible<Derived2, Derived1>, bool> operator==(
  135. Derived1 const& lhs
  136. , Derived2 const& rhs
  137. )
  138. {
  139. return static_cast<Derived1 const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
  140. }
  141. template<
  142. class Derived1
  143. , class Derived2
  144. >
  145. enable_if<is_convertible<Derived1, Derived2>, bool> operator==(
  146. Derived1 const& lhs
  147. , Derived2 const& rhs
  148. )
  149. {
  150. return static_cast<Derived2 const&>(rhs).equal_to(static_cast<Derived1 const&(lhs));
  151. }
  152. Given our simple and obvious definition of Mutable and Constant nothing has changed yet. ::
  153. c == m; // ok, dispatched to Constant::equal_to, m converted to Constant
  154. m == c; // ok, dispatched to Constant::equal_to, m converted to Constant
  155. But now the user can avoid the type conversion by supplying the
  156. appropriate overload in Constant ::
  157. struct Constant : Facade<Constant>
  158. {
  159. Constant();
  160. Constant(Constant const&);
  161. Constant(Mutable const&);
  162. ...
  163. bool equal_to(Constant const&);
  164. bool equal_to(Mutable const&);
  165. };
  166. c == m; // ok, dispatched to Constant::equal_to(Mutable const&), no conversion
  167. m == c; // ok, dispatched to Constant::equal_to(Mutable const&), no conversion
  168. This definition of operator== introduces a possible ambiguity when both types are convertible
  169. to each other. I don't think this is a problem as this behaviour is the same with concrete types.
  170. I.e. ::
  171. struct A {};
  172. bool operator==(A, A);
  173. struct B { B(A); };
  174. bool operator==(B, B);
  175. A a;
  176. B b(a);
  177. a == b; // error, ambiguous overload
  178. Effect
  179. ======
  180. Iterator implementations using iterator_facade look exactly as if they were
  181. "hand-implemented" (I am working on better wording).
  182. a) Less burden for the user
  183. b) The definition (standardese) of specialized adpters might be easier
  184. (This has to be proved yet)