ctEval

Enforces the evaluation of an expression during compile-time.

Computes the value of an expression during compilation (CTFE).

This is useful for call chains in functional programming where declaring an enum constant would require splitting the pipeline.

enum ctEval (
alias expr
)

Parameters

expr

expression to evaluate

Examples

import std.math : abs;

// No explicit `enum` needed.
float result = ctEval!(abs(-3));
assert(result == 3);

// Can be statically asserted.
static assert(ctEval!(abs(-4)) == 4);
static assert(ctEval!(abs( 9)) == 9);
import core.stdc.math : round;
import std.conv : to;
import std.math : abs, PI, sin;

// `round` from the C standard library cannot be interpreted at compile
// time, because it has no available source code. However the function
// calls preceding `round` can be evaluated during compile time.
int result = ctEval!(abs(sin(1.0)) * 180 / PI)
    .round()
    .to!int();

assert(result == 48);

See Also

Meta