Policy-oriented programming.

Gor F. Gyolchanyan gor.f.gyolchanyan at gmail.com
Sun Sep 25 07:49:08 PDT 2011


Hi, D.learn!

I encountered a problem, while i was designing a fast, performance-critical
image storage mechanism.
Here's what i want to do:
Have a FragmentTraits struct, which contains fields, like number of
components, size in bytes of the components, padding of the components in
bytes, etc.
Have a Fragment struct, which takes a FragmentTraits object as a template
parameter and defines the fragment components as it's fields according to
given traits.
Have a BufferTraits struct, which contains fields, like row padding in bytes,
etc.
Have a Buffer class, which takes a Fragment type and a BufferTraits object and
defines appropriate storage and indexing routines.

I know, structs are CTFE-able, so there should be no problem reading the
fields of those compile-time defined struct objects.
However, D does not allow me to have structs as template parameters and if I
define them as alias parameters, it refuses to read the fields at compile-time.

Having the user pass all those fields separately will lead to a very ugly API
and having the user define a template, containing enum-constants as fields
will lead to unnecessarily complicated and difficult interface and additional
templates to test, whether the templates are defined correctly or not.

How do i work around this limitation and are there any plans to include
structs in the list of allowed template parameters?


More information about the Digitalmars-d-learn mailing list