-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
1137 lines (798 loc) · 26.1 KB
/
index.html
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Type Erasure</title>
<meta name="author", content="T. Zachary Laine">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="presentation/css/reveal.css">
<link rel="stylesheet" href="presentation/css/theme/default.css" id="theme">
<link rel="stylesheet" href="presentation/lib/css/zenburn.css">
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'presentation/css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<script src="presentation/lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown=""
data-separator="^====+$"
data-vertical="^----+$"
data-notes="^Note:">
<script type="text/template">
# Type Erasure
## Solving Classic OOP Problems with an Elegant Design Pattern
### Zach Laine
====================
## Outline
- The Importance of Values
- Why Polymorphism?
- Those Other Solutions
- Type Erasure
----
## A Quick Aside
All the code you will see in this presentation compiles and works as
advertized.
====================
# Part 1
## The Importance of Values
----
Dealing with values instead of references has a couple of very nice benefits
- Clear ownership/lifetime semantics
- Equational reasoning
----
## Clear Semantics
Who owns `foo`?
```cpp
foo_t * foo_factory ();
foo_t * foo = foo_factory();
```
Is this better?
<!-- .element: class="fragment" data-fragment-index="1" -->
```cpp
std::shared_ptr<foo_t> foo_factory ();
std::shared_ptr<foo_t> foo = foo_factory();
```
<!-- .element: class="fragment" data-fragment-index="1" -->
That depends on whether you're partial to global state....
<!-- .element: class="fragment" data-fragment-index="2" -->
----
## Equational Reasoning
```cpp
foo_t * foo_factory ();
void foo_user (foo_t * f);
void some_function () {
foo_t * foo = foo_factory();
foo_user(foo);
// What can I say about foo here?
/* more code ... */
}
```
With value types, you only have to reason about the values in the code front
of you.
With reference types, you must **simultaneously** reason about code that
mutates the values elsewhere.
====================
# Part 2
## Why Polymorphism?
### Code reuse of course!
<!-- .element: class="fragment" data-fragment-index="1" -->
----
Q: How does the author of this function intend its users to use it?
```cpp
bool my_predicate (some_t obj);
```
A: Non-polymorphically. You have to give me a <!-- .element: class="fragment" data-fragment-index="1" -->`some_t` object, or get sliced.
Not terribly reusable with other types.
<!-- .element: class="fragment" data-fragment-index="1" -->
----
Q: How about this one?
```cpp
bool my_predicate (const base_t & obj);
```
A: Runtime-polymorphically. You can give me any <!-- .element: class="fragment" data-fragment-index="1" -->
`base_t`-derived object by
reference. The function is now more reusable, but you must use inheritance.
Yuck!<!-- .element: class="fragment" data-fragment-index="1" -->
----
Q: Or this?
```cpp
template <typename T>
bool my_predicate (T obj);
```
A: Compile-time-polymorphically. You can give me any object whose type can be
used to instantiated the template. The function is now even more reusable,
but you must use metaprogramming. Gross!
<!-- .element: class="fragment" data-fragment-index="1" -->
----
Q: Is this an example of polymorphism?
```cpp
struct base { virtual ~base () {} };
struct derived : base { virtual int foo () { return 42; } };
void some_function () {
base * b_pointer = new derived;
requires_foo(static_cast<derived*>(b_pointer)); // <-- this here
}
```
A: No! Polymorphism means using one type as if it were another type. Here,
we have to cast to make the type we have look like the type expected in the
interface to <!-- .element: class="fragment" data-fragment-index="1" -->`foo()`.
<!-- .element: class="fragment" data-fragment-index="1" -->
====================
# Part 3
## Those Other Solutions
- Inheritance-Based Runtime polymorphism
- Template-based Compile-time polymorphism
====================
## (Problems with) Inheritance as Runtime Polymorphism
----
The inheritance mechanism gives us polymorphism, but we must either:
- Limit ourselves to a single interface found in the base class:
```cpp
struct base
{ virtual int foo () const = 0; };
struct derived : base
{
virtual int foo () const
{ return 42; }
float bar () const
{ return 3.0f; }
};
void some_function () {
base * b_pointer = new derived;
uses_foo(b_pointer); // Yay!
// uses_bar(b_pointer); // Aww...
}
```
----
Note that the "base" could use multiple inheritance, as long as the symbols in
all the multiply inherited bases do not collide.
----
*OR*
----
- Use multiple inheritance to bolt on multiple interfaces:
```cpp
struct base { virtual ~base() {} };
struct has_foo : virtual base { virtual int foo () { return 42; } };
struct has_bar : virtual base { virtual float bar () { return 3.0f; } };
struct derived : has_foo, has_bar {};
void some_function () {
base * b_pointer = new derived;
uses_foo(dynamic_cast<has_foo*>(b_pointer)); // Wait, but...
uses_bar(dynamic_cast<has_bar*>(b_pointer)); // Aww...
}
```
Which leads to the "diamond of death",
<!-- .element: class="fragment" data-fragment-index="1" -->
which leads to virtual inheritance,
<!-- .element: class="fragment" data-fragment-index="2" -->
which leads to fear,
<!-- .element: class="fragment" data-fragment-index="3" -->
which leads to anger,
<!-- .element: class="fragment" data-fragment-index="4"-->
... you get it.
<!-- .element: class="fragment" data-fragment-index="5" -->
More importantly, we have actually given up polymorphism by doing this; we
must carry around the information that <!-- .element: class="fragment"
data-fragment-index="6" --> `b_pointer` contains interfaces not found in
`base`.
----
We cannot make classes from different class hierarchies conform to a common
interface, because they have no common base to use when passing them.
```cpp
struct int_foo
{
virtual int foo () { return 42; }
virtual void log () const;
};
struct float_foo
{
virtual float foo () { return 3.0f; }
virtual void log () const;
};
```
Note that `int_foo` and `float_foo` both have a member
`void log() const`.
----
So, we ditch code reuse in the case of logging:
```cpp
void log_to_terminal (const int_foo & loggee);
void log_to_terminal (const float_foo & loggee);
```
Or we resort to ugly hacks:
```cpp
struct log_base { virtual void log () const; };
struct int_foo : log_base {/*...*/};
struct float_foo : log_base {/*...*/};
void log_to_terminal (const log_base & loggee);
```
----
We cannot easily separate interface and implementation without using multiple
inheritance.
----
Virtual functions can be tricky to get right, especially in large class
heirarchies.
- We've all seen lots of these problems in real code.
- C++11's `override` and `final` help.
- There is frequently a question of whether or not a given type's virtual
function implementation should call its base class's version of the same
function.
----
In addition to all the above limitations, we are limited in which interfaces
we give to which types by our choice of base class(es).
Inheritance imposes very tight coupling; it is not possible to have unrelated
types with the same interfaces used interchangably.
----
We must always take parameters by reference to use runtime polymorphism.
There go our nice benefits from value semantics.
----
### A Quick Case Study: Widgets and Layouts
Widgets are UI elements (buttons, text boxes, etc.).
<!-- .element: class="fragment" data-fragment-index="1" -->
Layouts place widgets in the UI.
<!-- .element: class="fragment" data-fragment-index="2" -->
A layout can contain widgets.
<!-- .element: class="fragment" data-fragment-index="3" -->
A layout can contain sublayouts.
<!-- .element: class="fragment" data-fragment-index="4" -->
Using inheritance, you are all but locked in to giving layouts and widgets a
common base,
<!-- .element: class="fragment" data-fragment-index="5" -->
... even though that makes no sense.
<!-- .element: class="fragment" data-fragment-index="6" -->
====================
## (Problems with) Templates as Compile-time Polymorphism
----
### The Classic Problems
- Metaprogramming requires a large body of knowledge about a large number of
obscure language rules, and even more obscure TMP-specific tricks.
- Metaprogramming heavy code is hard to maintain. This is true for experts,
but is moreso in a team of varying skill levels.
- Metaprogramming might be simply impossible to use where you work (even if
you wanted to).
- Compile times and object code size can get away from you if you're not
careful.
----
Does not play well with runtime variation.
Compile-time is easy:
```cpp
template <typename TrueType, typename FalseType, bool Selection>
typename std::conditional<Selection, TrueType, FalseType>::type
factory_function () {
return
typename std::conditional<Selection, TrueType, FalseType>::type();
}
int an_int = factory_function<int, float, true>();
float a_float = factory_function<int, float, false>();
```
Runtime is impossible (without type erasure):
```cpp
template <typename TrueType, typename FalseType>
auto factory_function (bool selection) -> /* ??? */
{ return /* ??? */; }
```
----
Because of the lack of easy runtime interoperability, once you decide to use
TMP, you're almost always stuck doing more TMP.
====================
## There **must** be a better way!
![](presentation/there_must_be_a_better_way.gif)
====================
# Part 4
## Type Erasure
----
Based on everything we've seen so far, we want an interface that works like
this:
```cpp
// No coupling!
// No virtual functions, kind of!
struct foo { int value () const; };
struct bar { int value () const; };
// No templates!
int value_of (magic_type obj)
{ return obj.value(); }
void some_function () {
// Value semantics!
if (value_of(foo()) == value_of(bar())) {
/*...*/
}
}
```
----
### Making magic happen
```cpp
struct anything
{
anything () = default;
anything (const anything & rhs);
anything & operator= (const anything & rhs);
template <typename T> anything (T t);
template <typename T> anything & operator= (T t);
struct handle_base
{
virtual ~handle_base () {}
virtual handle_base * clone () const = 0;
};
template <typename T>
struct handle : handle_base
{
handle (T value);
virtual handle_base * clone () const;
T value_;
};
std::unique_ptr<handle_base> handle_;
};
```
----
`anything` definitions
```cpp
template <typename T>
anything::anything (T t) :
handle_ (new handle<typename std::remove_reference<T>::type>(
std::forward<T>(t)
))
{}
anything::anything (const anything & rhs) :
handle_ (rhs.handle_->clone())
{}
template <typename T>
anything & anything::operator= (T t)
{
anything temp(std::forward<T>(t));
std::swap(temp, *this);
return *this;
}
anything & anything::operator= (const anything & rhs)
{
anything temp(rhs);
std::swap(temp, *this);
return *this;
}
```
----
`anything::handle` definitions
```cpp
template <typename T>
anything::handle<T>::handle (T value) :
value_ (std::move(value))
{}
template <typename T>
anything::handle_base * anything::handle<T>::clone () const
{ return new handle(value_); }
```
----
`anything` in action
```cpp
int i = 1;
int * i_ptr = &i;
anything a;
a = i;
a = i_ptr;
a = 2.0;
a = std::string("3");
struct foo {};
a = foo();
```
Dymamic typing ("duck typing"), similar to that in Python. Consider dumping
scripting languages for this.
That's not a joke.
----
Q: Can `anything` really hold anything?
A: No, but it can hold anything with a copy constructor. It should really be
called <!-- .element: class="fragment" data-fragment-index="2" -->`copyable`.
<!-- .element: class="fragment" data-fragment-index="2" -->
----
Ok, so how do we get back to this?
```cpp
// No coupling!
// No virtual functions, kind of!
struct foo { int value () const; };
struct bar { int value () const; };
// No templates!
int value_of (magic_type obj)
{ return obj.value(); }
void some_function () {
// Value semantics!
if (value_of(foo()) == value_of(bar())) {
/*...*/
}
}
```
----
Easy -- just forward the calls to `value()`:
```cpp
// anything gets:
int value () const
{ return handle_->value(); }
```
```cpp
// anything::handle_base gets:
virtual int value () const = 0;
```
```cpp
// anything::handle<T> gets:
virtual int value () const
{ return value_.value(); }
```
----
We can add any arbitrary API in the same fashion. It's easy but repetitive to
do so. More on that in a bit.
----
Say we have two disjoint APIs we wish to support.
One erased type for all widgets:
```cpp
struct widget
{
// some boilerplate ...
void render () const;
// more boilerplate ...
};
```
And one for all objects that can be used in our layout system:
```cpp
struct layoutable
{
// boilerplate ...
layout_geometry geometry () const;
// boilerplate ...
};
```
----
Here it is in use:
```cpp
struct button
{
void render () const;
layout_geometry geometry () const;
void set_label ();
void set_image ();
// etc.
};
void do_layout (layoutable l) {
layout_geometry geometry = l.geometry();
/* use geometry... */
}
void render_widget (widget w)
{ w.render(); }
void some_function () {
button b;
do_layout(b);
render_widget(b);
}
```
----
Q: What about performance?
A: It's complicated.
<!-- .element: class="fragment" data-fragment-index="2" -->
----
### Pointer-based Inheritance vs. Erased Types
Function call overhead is exactly the same.
How about heap allocations?
| Operation | Inheritance | Simple Type Erasure |
| ----------------------- |:-----------:|:-------------------:|
| Construct | Yes | Yes |
| Copy | No | Yes |
| Assign | No | Yes |
| Get Alternate Interface | No* | Yes |
\* `dynamic_cast<>` is not free, and is not consistent with polymorphism.
This chart also applies to copies of the underlying object.
====================
## Optimizing the Type Erasure Technique
----
### Step 1: Accept references
Instead of always copying the given value, accept `std::reference_wrapper`s.
```cpp
button b;
widget ref_widget(std::ref(b)); // Underlying object
widget cref_widget(std::cref(b)); // not copied.
```
----
Replace the `handle` constructor with these:
```cpp
template <typename U = T>
handle (T value,
typename std::enable_if<
std::is_reference<U>::value
>::type * = 0) :
value_ (value)
{}
template <typename U = T>
handle (T value,
typename std::enable_if<
!std::is_reference<U>::value,
int
>::type * = 0) noexcept :
value_ (std::move(value))
{}
```
----
Add this specialization:
```cpp
template <typename T>
struct handle<std::reference_wrapper<T>> :
handle<T &>
{
handle (std::reference_wrapper<T> ref) :
handle<T &> (ref.get())
{}
};
```
----
### Pointer-based Inheritance vs. Erased Types
Allocations did not change.
How about copies of the underlying object?
| Operation | Inh. | Simple TE | TE + ref |
| -------------- |:----:|:---------:|:---------:|
| Construct | Yes | Yes | No |
| Copy | No | Yes | No |
| Assign | No | Yes | No |
| Alt. Interface | No* | Yes | No |
All versions discussed hereafter include support for `std::reference_wrapper`.
----
### Step 2: Use a Copy-On-Write Wrapper
Instead of always copying our type erased objects, only copy them when they
are mutated.
```cpp
copy_on_write<widget> w_1(widget{button()});
copy_on_write<widget> w_2 = w_1; // No copy.
widget & mutable_w_2 = w_2.write(); // Copy happens here.
```
A nice benefit of using copy-on-write is thread safety.
----
### Pointer-based Inheritance vs. Erased Types
Allocations:
| Operation | Inh. | Simple TE | TE + COW |
| -------------- |:----:|:---------:|:---------:|
| Construct | 1 | 1 | 2 |
| Copy | 0 | 1 | 0 |
| Assign | 0 | 1 | 0 |
| Alt. Interface | 0* | 1 | 2 |
----
### Step 3: Integrate Copy-On-Write into Erased Types
Remove one set of allocations on construction by applying copy-on-write
directly to the `handle_` member.
```cpp
widget w_1 = button(); // Only 1 allocation here.
widget w_2 = w_1; // No copy.
```
Copies only happen when a non-const member function is called.
----
### Pointer-based Inheritance vs. Erased Types
Allocations:
| Operation | Inh. | Simple TE | TE w/COW |
| -------------- |:----:|:---------:|:---------:|
| Construct | 1 | 1 | 1 |
| Copy | 0 | 1 | 0 |
| Assign | 0 | 1 | 0 |
| Alt. Interface | 0* | 1 | 1 |
----
### Step 4: Apply the Small Buffer Optimization
```cpp
std::array<int, 1024> big_array;
anything small = 1; // No allocation to store an int.
anything ref = std::ref(big_array); // No allocation to store a std::ref().
anything large = big_array; // Allocation required here.
```
----
### Pointer-based Inheritance vs. Erased Types
Allocations for small<sup>†</sup>/large objects:
| Operation | Inh. | Simple TE | TE w/SBO |
| -------------- |:----:|:---------:|:---------:|
| Construct | 1/1 | 1/1 | 0/1 |
| Copy | 0/0 | 1/1 | 0/1 |
| Assign | 0/0 | 1/1 | 0/1 |
| Alt. Interface | 0/0* | 1/1 | 0/1 |
<sup>†</sup> `std::referece_wrapper` is always small.
----
### Step 5: Apply SBO and Integrated COW
```cpp
std::array<int, 1024> big_array;
anything small = 1; // No allocation to store an int.
anything ref = std::ref(big_array); // No allocation to store a std::ref().
anything large = big_array; // Allocation required here.
anything copied = large; // No allocations for copies.
```
----
### Pointer-based Inheritance vs. Erased Types
Allocations for small<sup>†</sup>/large objects:
| Operation | Inh. | Simple TE | TE w/SBO+COW|
| -------------- |:----:|:---------:|:-----------:|
| Construct | 1/1 | 1/1 | 0/1 |
| Copy | 0/0 | 1/1 | 0/0 |
| Assign | 0/0 | 1/1 | 0/0 |
| Alt. Interface | 0/0* | 1/1 | 0/1 |
<sup>†</sup> Again, `std::referece_wrapper` is always small.
Copies only happen when a non-const member function is called.
----
The final implementation is a bit long to show here.
====================
## A Quick Review
What have we gained versus using inheritance?
What have we lost?
----
### Gains
- Value semantics
- Never writing `new` or `delete`
- Ability to bind types to interfaces never seen at the time of the types'
writing, including multiple interfaces
- Thread safety via copy-on-write
- Elision of heap allocations for small types and references
----
### Losses
- Simplicity of implementation
- Thread safety come at the cost of some atomic operations and copies when
values are mutated
====================
## Another Option: Boost.TypeErasure
Boost.TypeErasure is the most robust library-based type erasure solution to
date.
- Uses metapgrogramming-based explicit v-table construction.
- Supports casts directly to the held type, à la Boost.Any.
- Supports free-function requirements.
- Supports operator requirements.
- Supports associated type requirements.
- Supports concept maps.
----
An example from the online docs:
```cpp
any<
mpl::vector<
copy_constructible<>,
typeid_<>,
incrementable<>,
ostreamable<>
>
> x(10);
++x;
std::cout << x << std::endl; // prints 11
```
----
An example of an erased type with a member function:
```cpp
BOOST_TYPE_ERASURE_MEMBER((has_push_back), push_back, 1)
void append_many(any<has_push_back<void(int)>, _self&> container) {
for(int i = 0; i < 10; ++i)
container.push_back(i);
}
```
----
### Caveats
- The per-object v-table can create large objects.
- All the metaprogramming can contribute to long compile times.
- The error messages will make your eyes bleed.
- The small buffer optimization is not supported.
- Writing `const` member function requirements is not straightforward.
====================
## Inheritance vs. Hand-Rolled Type Erasure vs. Boost.TypeErasure
----
Allocations for small/large/ref held values:
| Operation | Inh. | Optimized TE | Boost TE |
| -------------- |:-----:|:------------:|:--------:|
| Construct | 1/1/- | 0/1/0 | 1/1/0 |
| Copy | 0/0/- | 0/0/0 | 1/1/0 |
| Assign | 0/0/- | 0/0/0 | 1/1/0 |
| Alt. Interface | 0/0/-*| 0/1/0 | 1/1/0 |
All values are the same for copies of the underlying value, except for the
optimized hand-rolled version's binding to an alternate interface.
----
Space requirements, including held-object storage:
| Technique | Held Object Size | Space Requirement |
| -------------- |:------------------:|:------------------:|
| Inheritance | All | `P` + `O` |
| Hand-rolled TE | Small | `P` + `B` |
| Hand-rolled TE | Large | `P` + `B` + `O` |
| Boost TE | All | `FM` + `O` |
`P` is the size of a pointer to a heap-allocated object, `F` is the size of a
pointer-to-function, `O` is the size of the stored object, `M` is the number
of members in an erased type's interface, and `B` is small buffer size.
====================
## One More Thing ...
The biggest advantage of using Boost.TypeErasure over a hand-rolled
implementation is that your hands get a rest.
Q: How do we avoid large amounts of cut-and-pasted code?