@@ -2199,15 +2199,50 @@ void main()
2199
2199
$(H2 $(LNAME2 interpretation, Compile Time Function Execution (CTFE)))
2200
2200
2201
2201
$(P Functions which are both portable and free of global side-effects can be
2202
- executed at compile time. This is useful when constant folding
2203
- algorithms need to include recursion and looping. Compile time function
2204
- execution is subject to the following restrictions:
2202
+ executed at compile time. In certain contexts, such compile time execution
2203
+ is guaranteed. It is called Compile Time Function Execution (CTFE) then.
2204
+ The contexts that trigger CTFE are:)
2205
+
2206
+ $(UL
2207
+ $(LI initialization of a static variable or a
2208
+ $(DDSUBLINK spec/enum, manifest_constants, manifest constant))
2209
+ $(LI static initializers of struct/class members)
2210
+ $(LI dimension of a $(DDSUBLINK spec/arrays, static-arrays, static array))
2211
+ $(LI argument for a $(DDSUBLINK spec/template, template_value_parameter,
2212
+ template value parameter))
2213
+ $(LI $(DDSUBLINK spec/version, staticif, `static if`))
2214
+ $(LI $(DDSUBLINK spec/version, staticforeach, `static foreach`))
2215
+ $(LI $(DDSUBLINK spec/version, static-assert, `static assert`))
2216
+ $(LI $(DDSUBLINK spec/statement, mixin-statement,
2217
+ `mixin` statement))
2218
+ $(LI $(DDLINK spec/pragma, Pragmas, `pragma` argument))
2205
2219
)
2206
2220
2221
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2222
+ ---
2223
+ enum eval(Args...) = Args[0];
2224
+
2225
+ int square(int i)
2226
+ {
2227
+ return i * i;
2228
+ }
2229
+
2230
+ void foo()
2231
+ {
2232
+ static j = square(3); // CTFE
2233
+ writeln(j);
2234
+ assert(square(4)); // run time
2235
+ writeln(eval!(square(5))); // CTFE
2236
+ }
2237
+ ---
2238
+ )
2239
+
2240
+ $(P CTFE is subject to the following restrictions:)
2241
+
2207
2242
$(OL
2208
2243
$(LI The function source code must be available to the compiler. Functions
2209
2244
which exist in the source code only as $(D_KEYWORD extern) declarations
2210
- cannot be executed at compile time .)
2245
+ cannot be executed in CTFE .)
2211
2246
2212
2247
$(LI Executed expressions may not reference any global or local
2213
2248
static variables.)
@@ -2217,7 +2252,7 @@ $(H2 $(LNAME2 interpretation, Compile Time Function Execution (CTFE)))
2217
2252
$(LI Non-portable casts (eg, from $(D int[]) to $(D float[])), including
2218
2253
casts which depend on endianness, are not permitted.
2219
2254
Casts between signed and unsigned types are permitted)
2220
- $(LI Reinterpretation of overlapped fields in a Union.)
2255
+ $(LI Reinterpretation of overlapped fields in a Union is not permitted .)
2221
2256
)
2222
2257
2223
2258
$(P Pointers are permitted in CTFE, provided they are used safely:)
@@ -2289,47 +2324,14 @@ static assert(countTen(6) == 6); // OK
2289
2324
static assert(countTen(12) == 12); // invalid, modifies y.
2290
2325
---
2291
2326
$(P The $(D __ctfe) boolean pseudo-variable, which evaluates to $(D_KEYWORD true)
2292
- at compile time , but $(D_KEYWORD false) at run time , can be used to provide
2327
+ in CTFE , but $(D_KEYWORD false) otherwise , can be used to provide
2293
2328
an alternative execution path to avoid operations which are forbidden
2294
- at compile time . Every usage of $(D __ctfe) is evaluated before
2329
+ in CTFE . Every usage of $(D __ctfe) is evaluated before
2295
2330
code generation and therefore has no run-time cost, even if no optimizer
2296
2331
is used.
2297
2332
)
2298
2333
2299
-
2300
- $(P In order to be executed at compile time, the function
2301
- must appear in a context where it must be so executed, for
2302
- example:)
2303
-
2304
- $(UL
2305
- $(LI initialization of a static variable or a manifest constant)
2306
- $(LI dimension of a static array)
2307
- $(LI argument for a template value parameter)
2308
- )
2309
-
2310
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
2311
- ---
2312
- template eval( A... )
2313
- {
2314
- const typeof(A[0]) eval = A[0];
2315
- }
2316
-
2317
- int square(int i)
2318
- {
2319
- return i * i;
2320
- }
2321
-
2322
- void foo()
2323
- {
2324
- static j = square(3); // compile time
2325
- writeln(j);
2326
- writeln(square(4)); // run time
2327
- writeln(eval!(square(5))); // compile time
2328
- }
2329
- ---
2330
- )
2331
-
2332
- $(P Executing functions at compile time can take considerably
2334
+ $(P Executing functions via CTFE can take considerably
2333
2335
longer than executing it at run time.
2334
2336
If the function goes into an infinite loop, it will hang at
2335
2337
compile time (rather than hanging at run time).
@@ -2339,7 +2341,7 @@ void foo()
2339
2341
throw exceptions; instead, they end interpretation immediately.
2340
2342
)
2341
2343
2342
- $(P Functions executed at compile time can give different results
2344
+ $(P Functions executed via CTFE can give different results
2343
2345
from run time in the following scenarios:
2344
2346
)
2345
2347
@@ -2357,7 +2359,7 @@ void foo()
2357
2359
2358
2360
$(H3 $(LNAME2 string-mixins, String Mixins and Compile Time Function Execution))
2359
2361
2360
- $(P Any functions that execute at compile time must also
2362
+ $(P Any functions that execute in CTFE must also
2361
2363
be executable at run time. The compile time evaluation of
2362
2364
a function does the equivalent of running the function at
2363
2365
run time. This means that the semantics of a function cannot
@@ -2372,11 +2374,13 @@ int foo(char[] s)
2372
2374
const int x = foo("1");
2373
2375
---
2374
2376
2375
- $(P is illegal, because the runtime code for foo() cannot be
2377
+ $(COMMENT Intentionally not a $(P ...) so that it doesn't get a
2378
+ paragraph number, because this continues the paragraph above.)
2379
+ is illegal, because the runtime code for `foo` cannot be
2376
2380
generated. A function template would be the appropriate
2377
- method to implement this sort of thing.)
2381
+ method to implement this sort of thing.
2378
2382
2379
- $(H3 $(LNAME2 nogc-functions, No-GC Functions))
2383
+ $(H2 $(LNAME2 nogc-functions, No-GC Functions))
2380
2384
2381
2385
$(P No-GC functions are functions marked with the $(D @nogc) attribute.
2382
2386
Those functions do not allocate memory on the GC heap,
0 commit comments