On Wednesday, 30 October 2013 at 20:23:58 UTC, Ali Çehreli wrote:
On 10/30/2013 01:11 PM, Craig Dillabaugh wrote:

> I am writing code that uses a structure containing an array of
> points where the points may be of arbitrary dimension (though
> generally small).  I would like to be able to pass the point
> dimension to my structure as a template parameter.

struct Point
{}

struct S(size_t N)
{
    Point[N] points;
}

alias S2D = S!2;
alias S3D = S!3;

void main()
{}

> One solution is to create instances of these structures for
all
> reasonable dimensions and then select the correct instance at
run
> time I suppose.

Do you need a common interface, or all of the algoritms will be templatized as well? In other words, what is the type that the following function returns?

??? selectInstance(size_t N)
{
    // return S2D or S3D?
}

You can have an interface that the template implements:

interface SInterface
{
    void foo();
}

class S(size_t N) : SInterface
{
    Point[N] points;

    void foo() { /* implementation for N */ }
}

Now selectInstance() returns SInterface:

SInterface selectInstance(size_t N)
{
    // ...
}

> However, I don't really want to set a limit on
> the point dimension.

All of the instances must be known at compile time. So, your program is always limited with the number of actual instances that the program is using.

Ali

Ali thanks.

The algorithms will be templatized as well.  As you point out, I
need to know all instances at compile time, but want to 'get
around' this, thus my idea of compiling the specific instance I
need.  Adam's idea should work.

I like the interface idea, but just to make sure I understand the
purpose - you use the interface to provide a common interface so
that the instantiated objects can be used by algorithms which do
not have any knowledge of the particular dimension of a given
point.  Is that correct?

Craig

Reply via email to