Template specialized functions creating runtime instructions?

Patrick Schluter Patrick.Schluter at bbox.fr
Wed Aug 21 10:01:48 UTC 2019


On Wednesday, 21 August 2019 at 00:11:23 UTC, ads wrote:
> On Wednesday, 21 August 2019 at 00:04:37 UTC, H. S. Teoh wrote:
>> On Tue, Aug 20, 2019 at 11:48:04PM +0000, ads via 
>> Digitalmars-d-learn wrote: [...]
>> 2) Deducing the string as you describe would require CTFE 
>> (compile-time function evaluation), which usually isn't done 
>> unless the result is *required* at compile-time.  The typical 
>> way to force this to happen is to store the result into an 
>> enum:
>>
>> 	enum myStr = fizzbuzz!...(...);
>> 	writeln(myStr);
>>
>> Since enums have to be known at compile-time, this forces CTFE 
>> evaluation of fizzbuzz, which is probably what you're looking 
>> for here.
>>
>> T
>
> Thank you for clearing those up. However even if I force CTFE 
> (line 35), it doesn't seem to help much.
>
> https://godbolt.org/z/MytoLF

It does.

on line 4113 you have that string

.L.str:
         .asciz  
"Buzz\n49\nFizz\n47\n46\nFizzBuzz\n44\n43\nFizz\n41\nBuzz\nFizz\n38\n37\nFizz\nBuzz\n34\nFizz\n32\n31\nFizzBuzz\n29\n28\nFizz\n26\nBuzz\nFizz\n23\n22\nFizz\nBuzz\n19\nFizz\n17\n16\nFizzBuzz\n14\n13\nFizz\n11\nBuzz\nFizz\n8\n7\nFizz\nBuzz\n4\nFizz\n2\n1\n"

and all main() does is call writeln with that string

_Dmain:
         push    rax
         lea     rsi, [rip + .L.str]
         mov     edi, 203
         call    @safe void 
std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])@PLT
         xor     eax, eax
         pop     rcx
         ret


You haven't given instruction to the linker to strip unused code 
so the functions generated by the templates are still there.


More information about the Digitalmars-d-learn mailing list