Arrays template arguments and CT data structures

bearophile bearophileHUGS at lycos.com
Fri Oct 2 13:56:48 PDT 2009


(after a small discussion on IRC) Tuples may be used for similar purposes, but fixed-sized arrays are simpler to use, simpler to define, and they don't induce compilation/code bloat. Is this a good idea?

int foo(int[3] arr)() {
    return arr[1];
}
const int[3] a = [10, 20, 30];
void main() {
    foo!(a)(); // a must be a constant fixed-sized array
}

--------------

Related:
A good purpose for compile-time functions is to pre-generate constant data structures, avoiding to waste time generating them at compile time. But currently creating fixed-sized arrays at compile time while possible is tricky (it's easy for me to write code that doesn't compile. So I'd like D2 to become more flexible here).
Constant associative arrays defined at compile time may use perfect hashing functions that can be used at run time.
And generating structures with pointers is not possible at compile time (possible usage for such structure: a trie that stores a constant dictionary, avoiding both build and load time, even if loading the precomputed chunk of memory of the array at run time takes a short time). At compile time indexes can be used instead of pointers, for example allocating the trie nodes into a compile time array and then using indexes to link nodes together.

Bye,
bearophile



More information about the Digitalmars-d mailing list