Practical difference between template "alias" arguments/normal generic arguments in this case?

Juanjo Alvarez via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 12 04:06:13 PDT 2017


Hi!

With "alias this" accepting runtime variables I'm struggling to 
understand the difference between a generic function with an 
"alias this" parameter and another one with a "runtime" parameter 
of template type.

Example:

// ---- example code ----
import std.stdio: writeln;

void writevalue1(alias param)() { writeln(param); }

void writevalue2(T)(T param) { writeln(param); }

void main() {
   import std.random: uniform;
   auto someNum = uniform(0, 1000); // runtime value
   writevalue1(someNum);
   someNum = uniform(0, 1000);
   writevalue2(someNum);
}
// ---- example end -----

Since both versions work with runtime values, what's are the 
differences? When I should prefer one version over the other?

If objdump is not lying to me, both calls jump to the same 
assembly and the only diffence is that the call to writevalue1 
does a "mov -0x8(%rdi),%edi" just before the callq instruction.


More information about the Digitalmars-d-learn mailing list