File: interoperability-revisited.rst

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (232 lines) | stat: -rw-r--r-- 6,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
++++++++++++++++++++++++++++
 Interoperability Revisited 
++++++++++++++++++++++++++++

:date: $Date: 2004/01/06 19:37:17 $
:copyright: Copyright Thomas Witt 2004.


Problem
=======

The current iterator_facade specification makes it unneccessarily tedious to
implement interoperable iterators.

In the following text a simplified example of the current iterator_facade specification is used to
illustrate the problem.

In the current specification binary operators are implemented in the following way:

template <class Derived>
struct Facade
{
};

template <class T1, T2>
struct is_interoperable :
  or_< 
       is_convertible<T1, T2>
     , is_convertible<T2, T1>
  > 
{};

template<
    class Derived1
  , class Derived2
>
enable_if<is_interoperable<Derived1, Derived2>, bool> operator==(
    Derived1 const& lhs
  , Derived2 const& rhs
)
{
  return static_cast<Derived1 const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
} 

The problem with this is that operator== always forwards to Derived1::equal_to. The net effect is that the
following "obvious" implementation of to interoperable types does not quite work.

struct Mutable : Facade<Mutable>
{
  bool equal_to(Mutable const&);  
};

struct Constant : Facade<Constant>
{
  Constant();
  Constant(Constant const&);
  Constant(Mutable const&);
 
  ...

  bool equal_to(Constant const&);  
};

Constant c;
Mutable  m;

c == m; // ok, dispatched to Constant::equal_to
m == c; // !! error, dispatched to Mutable::equal_to

Instead the following "slightly" more complicated implementation is neccessary

struct Mutable : Facade<Mutable>
{
  template <class T>
  enable_if<is_convertible<Mutable, T> || is_convertible<T, Mutable>, bool>::type equal_to(T const&);  
};

struct Constant : Tag<Constant>
{
  Constant();
  Constant(Constant const&);
  Constant(Mutable const&);

  template <class T>
  enable_if<is_convertible<Constant, T> || is_convertible<T, Constant>, bool>::type equal_to(T const&);  
};

Beside the fact that the code is significantly more complex to understand and to teach there is
a major design problem lurking here. Note that in both types equal_to is a function template with 
an unconstrained argument T. This is neccessary so that further types can be made interoperable with
Mutable or Constant. Would Mutable be defined as  

struct Mutable : Facade<Mutable>
{
  bool equal_to(Mutable const&);  
  bool equal_to(Constant const&);  
};

Constant and Mutable would still be interoperable but no further interoperable could be added 
without changing Mutable. Even if this would be considered acceptable the current specification forces
a two way dependency between interoperable types. Note in the templated equal_to case this dependency 
is implicitly created when specializing equal_to.

Solution
========

The two way dependency can be avoided by enabling type conversion in the binary operator
implementation. Note that this is the usual way interoperability betwween types is achieved
for binary operators and one reason why binary operators are usually implemented as non-members.

A simple implementation of this strategy would look like this

template<
    class T1
  , class T2
>
struct interoperable_base :
    if_< 
        is_convertible<
            T2
          , T1
        >
      , T1
      , T2>
{};


template<
    class Derived1
  , class Derived2
>
enable_if<is_interoperable<Derived1, Derived2>, bool> operator==(
    Derived1 const& lhs
  , Derived2 const& rhs
)
{
  typedef interoperable_base<
              Derived1
            , Derived2
          >::type Base;

  return static_cast<Base const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
} 

This way our original simple and "obvious" implementation would work again.

c == m; // ok, dispatched to Constant::equal_to
m == c; // ok, dispatched to Constant::equal_to, m converted to Constant

The backdraw of this approach is that a possibly costly conversion of iterator objects
is forced on the user even in cases where direct comparison could be implemented
in a much more efficient way. This problem arises especially for iterator_adaptor
specializations and can be significantly slow down the iteration over ranges. Given the fact
that iteration is a very basic operation this possible performance degradation is not 
acceptable.

Luckily whe can have our cake and eat it by a slightly more clever implementation of the binary 
operators.

template<
    class Derived1
  , class Derived2
>
enable_if<is_convertible<Derived2, Derived1>, bool> operator==(
    Derived1 const& lhs
  , Derived2 const& rhs
)
{
  return static_cast<Derived1 const&>(lhs).equal_to(static_cast<Derived2 const&(rhs));
} 

template<
    class Derived1
  , class Derived2
>
enable_if<is_convertible<Derived1, Derived2>, bool> operator==(
    Derived1 const& lhs
  , Derived2 const& rhs
)
{
  return static_cast<Derived2 const&>(rhs).equal_to(static_cast<Derived1 const&(lhs));
} 

Given our simple and obvious definition of Mutable and Constant nothing has changed yet. 

c == m; // ok, dispatched to Constant::equal_to, m converted to Constant
m == c; // ok, dispatched to Constant::equal_to, m converted to Constant

But now the user can avoid the type conversion by supplying the appropriate overload in Constant

struct Constant : Facade<Constant>
{
  Constant();
  Constant(Constant const&);
  Constant(Mutable const&);
 
  ...

  bool equal_to(Constant const&);  
  bool equal_to(Mutable const&);  
};

c == m; // ok, dispatched to Constant::equal_to(Mutable const&), no conversion
m == c; // ok, dispatched to Constant::equal_to(Mutable const&), no conversion

This definition of operator== introduces a possible ambiguity when both types are convertible
to each other. I don't think this is a problem as this behaviour is the same with concrete types.
I.e. 

struct A {};

bool operator==(A, A);

struct B { B(A); }; 

bool operator==(B, B);

A a;
B b(a);

a == b; // error, ambiguous overload

Effect
======

Iterator implementations using iterator_facade look exactly as if they were
"hand-implemented" (I am working on better wording).

a) Less burden for the user

b) The definition (standardese) of specialized adpters might be easier 
   (This has to be proved yet)