-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-nontype.html
executable file
·106 lines (88 loc) · 9.44 KB
/
auto-nontype.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
<!-- vim: set ts=2 sw=2 tw=80 fo=tc et -->
<!DOCTYPE html>
<html>
<head>
<title>auto as a placeholder for non-type template parameters</title>
<style>
code { white-space: pre; }
th { text-align: left; }
</style>
</head>
<body>
<h2><code>auto</code> as a placeholder for non-type template parameters</code></h2>
<table>
<tr>
<th>Document number:</th><td>P????R0</td>
</tr>
<tr>
<th rowspan=2>Authors:</th><td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<th>Date:</th><td>2016-10-07</td>
</tr>
<tr>
<th>Audience:</th><td>Evolution Working Group</td>
</tr>
</table>
<h3>Abstract</h3>
<p>This paper proposes an extension to the <a href="http://wg21.link/n4553">Concepts Technical Specification</a> to allow <code>auto</code> to act as a placeholder for non-type template parameters, just as it currently does for type template parameters.</p>
<p>Under this proposal, the following code would be valid:
<code>
template <size_t Bits> class bitset { /* ... */ };
bitset<32> foo();
bitset<auto> var = foo(); // auto is deduced to 32
</code></p>
<h3>Motivation</h3>
<p>In the <a href="http://wg21.link/n4553">Concepts TS</a>, a concept that constrains a type can be used as a placeholder for a type template parameter:
<code>
template <typename T>
concept bool TypeConcept = /* ... */;
template <typename T> class vector { /* ... */ };
vector<TypeConcept> var = /* ... */;
</code></p>
<p>and a concept that constraints a non-type can be used as a placeholder for a non-type template parameter:
<code>
template <size_t T>
concept bool NonTypeConcept = /* ... */;
template <size_t Bits> class bitset { /* ... */ };
bitset<TypeConcept> var = /* ... */;
</code></p>
<p>If you want the deduction provided by a placeholder, without any constraints, for a type template parameter you can use <code>auto</code>:
<code>
template <typename T> class vector { /* ... */ };
vector<auto> var = /* ... */;
</code></p>
<p>However, there is currently no equivalent for a non-type template parameter:
<code>
template <size_t Bits> class bitset { /* ... */ };
bitset<???> var = /* ... */; // what to put for ??? if you don't want any constraints?
</code></p>
You can, of course, write a concept that takes a <code>size_t</code> parameter and always returns <code>true</code> and use that, but it feels strange to have to do that for non-type template parameters, when for type template parameters you can just use <code>auto</code>.
<h3>Proposal</h3>
<p>The authors propose allowing <code>auto</code> to be used as a placeholder for a non-type template parameter as well.</p>
<p>For example:
<code>
template <size_t Bits> class bitset { /* ... */ };
bitset<32> foo();
bitset<auto> var = foo(); // auto is deduced to 32
</code></p>
<h3>Template template parameters</h3>
<p>For completeness and consistency, the authors propose allowing <code>auto</code> to be used as a placeholder for a template template parameter as well. This way, all template parameter kinds are treated the same, and <code>auto</code> in a placeholder context can be described simply as "deduce this parameter".</p>
<h3>Synergies with other proposals</h3>
<p><a href="http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0091r3.html">Template argument deduction for class templates</a> was accepted into C++17.</p>
<p>The <a href="http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0091r2.html">original version</a> of the proposal included support for partial deduction, such as:
<code>
template <typename T, typename U> class Foo { /* ... */ };
Foo<int> var{ /* ... */ }; // T is specified, U is deduced from the constructor arguments
</code></p>
<p>However, support for partial deduction was removed due to interactions with variadic templates.</p>
<p>One candidate solution was to allow partial deduction by using <code>auto</code> in place of the parameter we'd like deduced, instead of omitting it:
<code>
Foo<int, auto> var{ /* ... */ }; // T is specified, U is deduced
</code></p>
<p>This was rejected as a solution because currently, <code>auto</code> only acts as a placeholder for type template parameters. If this proposal were adopted, that would no longer be an issue.</p>
</body>
</html>