CTFE in betterC needs the GC

Ali Çehreli acehreli at yahoo.com
Sun Oct 30 04:14:17 UTC 2022


On 10/29/22 20:41, arandomonlooker wrote:

 > string exampleCTFE(string a, string b) {
 >      return a ~ b;
 > }

Although you need that function only at compile time, it is an ordinary 
function that could be called at run time as well. The function needs GC 
for that concatenation in the general case.

One way of solving your issue would be to change the function to a 
function template and pass the strings as template parameters 
(compile-time parameters):

string exampleCTFE(string a, string b)() {
     return a ~ b;
}

which would require explicit function template instantiation:

enum example = exampleCTFE!("A", "BCDE");

which would cause code bloat because exampleCTFE would be instantiated 
for every string combination that was used. For example, the following 
two uses would require two separate instantiations:

enum example1 = exampleCTFE!("A", "BCDE");
enum example2 = exampleCTFE!("A", "BCDEX");

 > int main() {

I think you also need to specify main() as extern(C):

extern (C)
int main() {
   // ...
}

Ali



More information about the Digitalmars-d-learn mailing list