Questions tagged [ctfe]

Compile Time Function Evaluation

Compile time function execution (or compile-time function evaluation, CTFE) is the ability of a compiler, that would normally compile a function to machine code and execute it at run-time, to execute the function at compile-time. This is possible if the arguments to the function are known at compile time, and the function does not make any reference to or attempt to modify any global state (is a pure function).

Even if the value of only some of the arguments are known, the compiler may still be able to perform some level of compile time function execution (partial evaluation), possibly producing more optimized code than if no arguments were known.

This technique present in the D programming language and in earlier versions of C++ template metaprogramming.

In C++11 the equivalent technique is known as Generalized constant expressions.

via: Wikipedia Article

15 questions
6
votes
3 answers

Why can't the compiler detect that functions are constant without annotating them with const?

In Rust, const functions are quite limited in what code can be put inside them, e.g. for loops aren't allowed, nor are any non-const function calls. I understand that there are issues with heap allocations in const functions, but why is it that for…
isaacholt100
  • 388
  • 3
  • 11
6
votes
1 answer

D CTFE and GPU Code Generation

Could D's Mixins be used to map linear algebra operations to either/both CPU code and OpenCL or GPU vertex shader functions such as GLSL? This would be a real killer application for D and better bridge logic targeted for both CPU and GPU execution.…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
5
votes
1 answer

D traits - List of integral data members

I am trying to use the following code to get a list of integral data members from class: import std.stdio; import std.traits; class D { static string[] integralMembers = getIntegralMembers(); static string[] getIntegralMembers() { …
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
4
votes
3 answers

Import content from filenames defined in an array

I can concatenate files read by import during compile time like this: enum string a = import("a.txt"); enum string b = import("b.txt"); enum string result = a ~ b; How can I get the concatenated result if I have the filenames in an array? enum…
Tamas
  • 3,254
  • 4
  • 29
  • 51
4
votes
1 answer

Evaluating Regular Expressions at compile time in D

Is there a way to evaluate regular expressions in D, at compile time?
Jeroen
  • 15,257
  • 12
  • 59
  • 102
3
votes
1 answer

D arbitrary code at compile time

I've heard D can execute arbitrary user code at compile time. Is this true? Could someone give an example (particularly when compilation never terminates)? What this feature is for? Also if that's so is there a way to disable this with some compiler…
ren
  • 3,843
  • 9
  • 50
  • 95
2
votes
1 answer

Memoization for compile time functions

I'd like to lazily evaluate functions. Since calculating the return values are expensive, I have to use memoization, especially for the called subfunctions, otherwise the calculation's time complexity explodes exponentially. I need the results at…
Tamas
  • 3,254
  • 4
  • 29
  • 51
2
votes
1 answer

How efficient is `typeof(expr)`?

How efficient is typeof when dealing with complex expressions? Specifically, in the two cases: complex expressions that consist entirely of constant parts needing no CTFE to evaluate complex expressions that need to mixin() a CTFE'ed string. I…
Noein
  • 522
  • 1
  • 6
  • 16
2
votes
1 answer

UDA opCall __traits

This code fails at the second unittest at the getA!B() call. The error is: "need 'this' for 'value' of type 'string'" The question is. How do I get getA to always return a A, whether the UDA is a type or an opCall? static A opCall(T...)(T args)…
burner
  • 323
  • 2
  • 10
2
votes
1 answer

Using CTFE to Generate Set of struct aliases

Say I have a class struct Vector (ElementType, uint Dimension) { ... } representing a fixed-dimensional vector along with these shorthands alias Vector!(float, 2) vec2; alias Vector!(float, 3) vec3; alias Vector!(float, 4) vec4; alias…
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
1
vote
1 answer

Implementing a C-style sizeof() function in D

I'd like to implement a C-style sizeof() function in D. (I know about the .sizeof thing, but it's to help in porting a lot of C (C99).) I want it to be able to run at compile-time, obviously and take a type or an expression as an argument. Ideally,…
Cecil Ward
  • 597
  • 2
  • 13
1
vote
1 answer

memset() and memcpy() using D slices

In the D language, what are the equivalents to the following statements assuming the code :- int size = 8; int shift = 1; int[size] skip; int[size] suff; memcpy(&skip[0], &skip[0]+shift, (m-shift)*(int.sizeof)); memset(&skip[0]+(m-shift),0,…
Walker
  • 323
  • 1
  • 12
1
vote
3 answers

Does C++ allow CTFE?

Tested a simple utf8 strlen function and was quite surprised that trunk clang completely eliminated it (gcc doesn't): static int strlenutf8(const char* s) { int i = 0, l = 0; while (s[i]) { if ((s[i] & 0xc0) != 0x80) l++; l++; } …
Trass3r
  • 5,858
  • 2
  • 30
  • 45
1
vote
2 answers

Joining strings at compile time

I want to join file names and image formats at compile time. The following example doesn't work, because string[] can't be evaluated at compile I suppose... immutable imageFormats = ["bmp", "jpg", "gif", "png"]; template…
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
0
votes
1 answer

Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D

I need advice on my first D-project . I have uploaded it at :- https://bitbucket.org/mrjohns/matcher/downloads IDEA : Benchmarking of 3 runtime algorithms and comparing them to their compile-time variants. The only difference between these is that…
Walker
  • 323
  • 1
  • 12