-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
249 lines (196 loc) · 8.54 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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>A Proposal to Add Base Class Aliases to the Language</title>
<style type="text/css">
#paper_details { float: right; }
#paper_details td { padding-left: 1em; }
h1 { clear: both; text-align: center; padding-top: 1em; }
.grammar, .syntax { font-style: italic; }
.grammar { margin-bottom: 2em; }
.grammar .definition { margin-top: 1em; }
.rule { padding-left: 3em; }
.literal, pre, code { font-style: normal; font-family: monospace; }
</style>
</head>
<body>
<table id="paper_details">
<tr><td>Document number:</td><td><em>not submitted</em></td></tr>
<tr><td>Date:</td><td>2014-03-27</td></tr>
<tr><td>Project:</td><td>JTC1.22.32 Programming Language C++</td></tr>
<tr><td>Reply-to:</td><td>Rouslan Korneychuk <rouslank at msn dot com></td></tr>
</table>
<h1>A Proposal to Add Base Class Aliases to the Language</h1>
<h2>I. Table of Contents</h2>
<ul>
<li><a href="#introduction">II. Introduction</a></li>
<li><a href="#motivation">III. Motivation</a></li>
<li><a href="#impact">IV. Impact On the Standard</a></li>
<li><a href="#design">V. Design Decisions</a></li>
<li><a href="#specification">VI. Technical Specifications</a></li>
<!-- <li><a href="#acknowledge">VII. Acknowledgements</a></li> -->
<li><a href="#references">VII. References</a></li>
</ul>
<h2 id="introduction">II. Introduction</h2>
<p>This proposal suggest adding a short-hand notation for creating aliases to
base classes within the scope of class.</p>
<p>e.g.:</p>
<pre>class A : protected <strong>B = C</strong>, public D {
public:
A() : B(3) {}
void do_something() {
/* ... */
B::do_something();
}
};</pre>
<p>which would be approximately equivalent to:</p>
<pre>class A : protected C, public D {
<strong>protected:
typedef C B;</strong>
public:
A() : B(3) {}
void do_something() {
/* ... */
B::do_something();
}
};</pre>
<p>The difference being that in the proposed syntax, the name <code>B</code>
is specified sooner and thus could be used in the second
<span class="syntax">base-specifier</span>. The name <code>B</code> would always
have the same accessibility as the name <code>C</code>.</p>
<h2 id="motivation">III. Motivation</h2>
<p>Most of the time, a class' base class can be easily referred to by its name
due to class name injection, but there are a few cases where this does not work
and if the base class is a template, requires either repeating template
parameters or a work-around.</p>
<h4>Dependent type names</h4>
<p>e.g.:</p>
<pre>
template<typename T,typename Pasta> class monstrous :
public something<
monstrous<T,Pasta>,
allocator<monstrous<T,Pasta>>,
comparison<monstrous<T,Pasta>,
and_so_on<Pasta>,
and_so_forth<T,etc<T>>> {
/* ... */
};
</pre>
<p>A very long instantiation such as this is particularly undesirable to have to
repeat. The injected name <code>something</code> cannot be used because the
definition of its class is not available before <code>monstrous</code> is
defined.</p>
<h4>Unknown class names</h4>
<p>e.g.:</p>
<pre>
// elsewhere:
template<typename Alloc> using particular_t = typename particular<Alloc>::type;
/* ... */
class A : public particular_t<my_allocator<double>> {
/* ... */
};
</pre>
<p>Here again the base class cannot be referred to by <code>particular_t</code>,
since that is not its name. Looking at the definition of
<code>particular_t</code> it looks like <code>type</code> is its name, but that
might not be so. <code>type</code> might actually be a
<span class="literal">typedef</span> to another name, and in fact, the actual
name of the class may vary depending on the template parameter.</p>
<h4>Ambiguous names</h4>
<pre>
class A : public B<X>, public B<Y> {
/* ... */
};
</pre>
<p>Here the name <code>B</code> is ambiguous.</p>
<h3>Alternatives</h3>
<p>The first example can be solved like so:</p>
<pre>
template<typename T,typename Pasta> class monstrous : public something</* ... */> {
typedef typename monstrous::something base_t;
/* ... */
};
</pre>
<p>This, however, will not solve the problem in the other two examples.</p>
<p>A particular template instantiation may be declared in a typedef before being
used in a derived class. Disadvantages: this pollutes the namespace with extra
names, and doesn't work in the first case. In the first case, one could forward
declare <code>monstrous</code> and declare a template alias with the same
parameters, but this would only exchange duplicating the base class' arguments
with duplicating the parameters of the derived class.</p>
<p>An extra template parameter can be added to the derived class to be used in
place of the base class, with a default value of the base class that is desired.
e.g.:</p>
<pre>template<typename T,typename B=some_base</*...*/>> class some_class : public B {
/* ... */
};</pre>
<p>Disadvantages: This solution is not obvious, doesn't work for the first case
(the names <code>B</code> and <code>some_class</code> are not available at that
point) and would require turning a plain class into a template if it wasn't one
already.</p>
<p>Work is underway to develop a reflection standard for C++, so in the future,
given a class <code>A</code>, one will probably be able to refer to—for
example— the first direct base class with something like
<code>std::base_t<A,0></code>. This however lacks the informativeness of a
meaningful name and is brittle, since adding, removing or reordering base
classes would change the numbering.</p>
<h3>With the new syntax</h3>
<p>The proposed syntax would allow resolving the issues in the examples like
so:</p>
<pre>
// example 1
template<typename T,typename Pasta> class monstrous :
public base_t = something<
monstrous<T,Pasta>,
allocator<monstrous<T,Pasta>>,
comparison<monstrous<T,Pasta>,
and_so_on<Pasta>,
and_so_forth<T,etc<T>>> {
/* ... */
};
// example 2
class A : public particular = particular_t<my_allocator<double>> {
/* ... */
};
// example 3
class A : public Bx = B<X>, public By = B<Y> {
/* ... */
};
</pre>
<h2 id="impact">IV. Impact On the Standard</h2>
<p>The proposed syntax is currently ill-formed and thus will not affect existing
code.</p>
<h2 id="design">V. Design Decisions</h2>
<p>The format is chosen for its similarity to the existing
<span class="syntax">alias-declaration</span>.</p>
<h2 id="specification">VI. Technical Specifications</h2>
<p>The following changes to the grammar are required:</p>
<h4>A.9 Derived classes <span class="gramsection">[gram.derived]</span></h4>
<div class="grammar">
<div class="definition">base-specifier:</div>
<div class="rule">attribute-specifier-seq<sub>opt</sub> <del>base-type-specifier</del> <ins>base-alias</ins></div>
<div class="rule">attribute-specifier-seq<sub>opt</sub> <span class="literal">virtual</span> access-specifier<sub>opt</sub> <del>base-type-specifier</del> <ins>base-alias</ins></div>
<div class="rule">attribute-specifier-seq<sub>opt</sub> access-specifier <span class="literal">virtual</span><sub>opt</sub> <del>base-type-specifier</del> <ins>base-alias</ins></div>
<div class="definition"><ins>base-alias:</ins></div>
<div class="rule"><ins>base-type-specifier</ins></div>
<div class="rule"><ins>identifier <span class="literal">=</span> base-type-specifier</ins></div>
<div class="definition">base-type-specifier:</div>
<div class="rule">class-or-decltype</div>
</div>
<p>A <span class="syntax">typedef-name</span> can be introduced with a
<span class="syntax">base-alias</span> if the
<span class="syntax">base-type-specifier</span> is preceded by an
<span class="syntax">identifier</span> and an equals sign. The semantics are the
same as if the name was defined with a <span class="literal">typedef</span> in
the base class, except it is only introduced into the scope of the derived class
and is only available after the <span class="syntax">base-alias</span> is
declared. The aliased name, therefore, has the same accessibility as the
inherited base class name.</p>
<!-- <h2 id="acknowledge">VII. Acknowledgements</h2> -->
<h2 id="references">VII. References</h2>
<p>The standards and terms are taken from the working draft
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf">N3797</a>.<br>
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf</a></p>
</body>
</html>