Buscar..


Observaciones

CTFE es un mecanismo que permite al compilador ejecutar funciones en tiempo de compilación. No hay un conjunto especial de lenguaje D necesario para usar esta función; cuando una función solo depende del tiempo de compilación de valores conocidos, el compilador D puede decidir interpretarlo durante la compilación.

También puedes jugar interactivamente con CTFE.

Evaluar una función en tiempo de compilación

long fib(long n)
{
    return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

struct FibStruct(int n) { // Remarks: n is a template
    ubyte[fib(n)] data;
}

void main()
{
    import std.stdio : writeln;
    enum f10 = fib(10); // execute the function at compile-time
    pragma(msg, f10); // will print 55 during compile-time
    writeln(f10); // print 55 during runtime
    pragma(msg, FibStruct!11.sizeof); // The size of the struct is 89
}


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow