are mixin string templates with functions removed?

JS js.mdnq at gmail.com
Thu Aug 1 15:04:56 PDT 2013


On Thursday, 1 August 2013 at 21:17:34 UTC, H. S. Teoh wrote:
> 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.
>


yes, I understand that... now use a template for a string 
mixin!!!!!

template A()
{
     string A() { ... }
}

...

mixin(A());

IS A GOING TO BE IN THE BINARY?!?!?! Yes, I'm yelling... just to 
get the point across about the question I'm trying to get 
answered.

the function A is never used at runtime SO it should technically 
not be in the binary UNLESS dmd treats it as a normal template 
function then it will(but shouldn't)!

e.g.,

if the compiler smart enough to realize that A(); is different 
from mixin(A());

(one being compile time and the other not)




More information about the Digitalmars-d-learn mailing list