@@ -124,33 +124,32 @@ $(H2 $(LNAME2 d-solutions, D Programming Language Solutions))
124
124
125
125
$(H3 The D Look Ma No Templates Solution)
126
126
127
- $(P It is not practical to solve this problem in C++ without
128
- using templates. In D, one can because D supports typesafe
129
- variadic function parameters.
127
+ $(P The current best practice, e.g. as espoused by the standard library, favours
128
+ the variadic template solution. However, if the template solution is not practical, D provides a runtime solution
129
+ utilizing the $(D TypeInfo) system (An example is shown below). There is a lexical similarity with the
130
+ non-template variadic functions provided by C and C++ ($(D va_list)), however the solution D provides is
131
+ typesafe whereas the aforementioned alternative is not.
130
132
)
131
133
132
134
------
133
135
import core.vararg;
134
136
import core.stdc.stdio;
135
- import std.format;
136
-
137
+ /*
138
+ This function was part of D1.0, but does not exist anymore, it has been
139
+ resurrected here to give a realistic example of how this feature could be used.
140
+ */
141
+ void doFormat(void delegate(dchar) putc, TypeInfo[] arguments, va_list argptr);
137
142
void print(...)
138
143
{
139
144
void putc(dchar c)
140
145
{
141
146
fputc(c, stdout);
142
147
}
143
148
144
- std.format. doFormat(&putc, _arguments, _argptr);
149
+ doFormat(&putc, _arguments, _argptr);
145
150
}
146
151
------
147
152
148
- $(P It isn't elegant or the most efficient,
149
- but it does work. However it is not the recommended way to write variadic functions.
150
- (It relies on the parameters `_argptr` and `_arguments` imported from `core.vararg`
151
- which give a pointer to the values and their types, respectively.)
152
- )
153
-
154
153
$(H3 Translating the Variadic C++ Solution into D)
155
154
156
155
$(P Variadic templates in D enable a straightforward translation
0 commit comments