Skip to content

Commit 0de0936

Browse files
authored
Fix Issue 21161 - [Variadic Templates] uses outdated example from D1 (#2998)
1 parent 46448eb commit 0de0936

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

articles/variadic-function-templates.dd

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,33 +124,32 @@ $(H2 $(LNAME2 d-solutions, D Programming Language Solutions))
124124

125125
$(H3 The D Look Ma No Templates Solution)
126126

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.
130132
)
131133

132134
------
133135
import core.vararg;
134136
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);
137142
void print(...)
138143
{
139144
void putc(dchar c)
140145
{
141146
fputc(c, stdout);
142147
}
143148

144-
std.format.doFormat(&putc, _arguments, _argptr);
149+
doFormat(&putc, _arguments, _argptr);
145150
}
146151
------
147152

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-
154153
$(H3 Translating the Variadic C++ Solution into D)
155154

156155
$(P Variadic templates in D enable a straightforward translation

0 commit comments

Comments
 (0)