Multiple template alias parameters
Artur Skawina via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat May 9 03:07:53 PDT 2015
On 05/08/15 23:56, Brian Schott via Digitalmars-d-learn wrote:
> On Friday, 8 May 2015 at 12:44:31 UTC, Artur Skawina wrote:
>> On 05/08/15 03:53, Brian Schott via Digitalmars-d-learn wrote:
>>> The problem occurs when I want to register multiple modules to scan for functions. The grammar does not allow this syntax:
>>>
>>> ```
>>> template (alias Modules ...) {
>>> ...
>>> ```
>>
>> The grammar allows omitting the 'alias' keyword.
>>
>> artur
>
> alias parameters are different from normal template parameters. They're not necessary for this problem, but they are for others.
I was trying to hint at the fact that D's template tuple parameters
already have the required magic.
Hence, the trailing '...' makes that 'alias' unnecessary.
> As an example:
>
>
> void traceVar(alias var, size_t line = __LINE__, string file = __FILE__)()
> {
> import std.stdio: stderr;
> stderr.writeln(file, "(", line, ") ", var.stringof, ": ", var);
> }
>
> This allows you to print a variable's name and value by only passing the variable once as a template argument. Allowing "template Tem(alias Args ...)" syntax would let me trace multiple variables at once.
>
> If you omit "alias", "var.stringof" evaluates to "var" instead of its name in the calling context.
template traceVar(VARS...) {
void traceVar(size_t line = __LINE__, string file = __FILE__)() {
import std.stdio: stderr;
foreach (I, ref var; VARS)
stderr.writeln(file, "(", line, ") ", VARS[I].stringof, ": ", var);
}
}
artur
More information about the Digitalmars-d-learn
mailing list