Template Question
Mike Parker
aldacron71 at yahoo.com
Sun Apr 13 23:06:07 PDT 2008
I've not worked much with templates in any language beyond the basics.
Right now, I've got a problem that I've solved, but for which I'm
looking a different solution.
What I have is a templated interface like so:
interface Foo(T)
{
void init(T);
void term();
void blah();
void bleh();
}
Then I have a Templated manager class intended to work with differently
parameterized Foos:
class Manager(U)
{
...
}
The issue is that I want to make sure that Manager is instantiated with
Foos without it knowing what the T in Foo is. This obviously doesn't work:
class Manager(U : Foo)
{
}
One way to solve the problem is to de-templatize Foo and create a new
interface like so:
interface FooInit
{
}
interface Foo
{
void init(FooInit fi);
}
I don't like that too much, though, since the parameters will always be
PODs, so I'd prefer to use structs and not classes for them. The
solution I've come up with, based on my limited knowledge, is to check
that the parameter U for the manager implements all of the functions
required by the Foo interface:
class Manager(U)
{
static this()
{
static if(!is(typeof(&U.init)) && !is(typeof(&U.term))
&& !is(typeof(&U.blah)) & !is(typeof(&U.bleh)));
}
}
This works fine for my purposes, even though it allows any type that
implements these methods, regardless of whether or not it implements Foo.
I'm curious if there is another way to do this, particularly some way to
make sure that Manager only accepts Foo implementations without knowing
how they are parameterized.
More information about the Digitalmars-d-learn
mailing list