are mixin string templates with functions removed?

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Aug 1 14:16:00 PDT 2013


On Thu, Aug 01, 2013 at 10:06:54PM +0200, JS wrote:
[...]
> Now are you telling me that
> 
> template A()
> {
>     void foo() { writeln("asdf"); }
> }
> void main()
> {
> 	A!().foo();
> }
> 
> does not create a function foo in the binary? That it is equivalent
> to just calling writeln("asdf"); directly? (exact same code)
[...]

I said that every instantiation of a template creates a copy of
everything inside. Therefore, A!().foo() will create a copy of A.foo()
in the binary.

The template itself has no binary representation, in the sense that if
you write:

	template A(int x) {
		void foo() { writeln(x); }
	}

there is nothing in the binary corresponding with the template A, or the
uninstantiated function foo. But if you instantiate A with some value of
x, then you will get a copy of A.foo for every value of x that you
instantiate the template with. So if you write:

	A!1.foo();
	A!2.foo();
	A!3.foo();

Then you will get 3 copies of foo() in your executable, one for each
value of x.


T

-- 
What do you get if you drop a piano down a mineshaft? A flat minor.


More information about the Digitalmars-d-learn mailing list