Set variable at compile time
Ali Çehreli
acehreli at yahoo.com
Wed Oct 30 13:23:58 PDT 2013
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
More information about the Digitalmars-d-learn
mailing list