View source code
Display the source code in std/functional.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Enum member std.functional.ctEval

Enforces the evaluation of an expression during compile-time.

enum ctEval(alias expr) = expr;

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.

Parameters

NameDescription
expr expression to evaluate

See also

https://dlang.org/spec/function.html#interpretation

Example

import std.math : abs;

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

// Can be statically asserted.
static assert(ctEval!(abs(-4)) == 4);
static assert(ctEval!(abs( 9)) == 9);

Example

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();

writeln(result); // 48

Authors

Andrei Alexandrescu

License

Boost License 1.0.