How to properly use variadic templates (functions)?
rempas
rempas at tutanota.com
Mon Dec 20 21:26:45 UTC 2021
I'm trying to implement "printf" and I'm getting an error. The
smallest possible code to demonstrate the error is:
```
import core.stdc.stdio;
void print(T...)(string prompt, T args) {
// Suppose all 'args' are of type "string" for this example
ulong carg = 0;
for (ulong i = 0; i < args[carg].length; i++) {
putc(args[carg][i], stdout);
}
}
void main() {
print("Prompt (ignored)", "Hello", " world!\n");
}
```
The error output I'm getting is:
```
test.d(6): Error: variable `carg` cannot be read at compile time
test.d(7): Error: variable `carg` cannot be read at compile time
test.d(12): Error: template instance `test.print!(string,
string)` error instantiating
```
There is another error in my original code. Let's see the
following line:
`u64 str_len = strlen(args[carg]); //u64 = ulong`
Now this should also give me the error that the variable "carg"
cannot be read at compile time (which it probably does as it
gives me an error when trying to use "str_len" inside a "for
loop") but instead it gives me the following error:
```
Error: function `core.stdc.string.strlen(scope const(char*) s)`
is not callable using argument types `(string)`
cannot pass argument `_param_1` of type `string` to
parameter `scope const(char*) s`
```
Any ideas?
More information about the Digitalmars-d-learn
mailing list