How to structure templated classes
Jacob Carlborg
doob at me.com
Wed May 9 13:48:59 PDT 2012
On 2012-05-09 22:21, Christian Köstlin wrote:
> I have a templated class that has many (static) inner classes which also
> use the template parameter. e.g.
> import std.stdio;
>
> class Test(T) {
> static class Inner1 : Test {
> T h;
> }
> static class Inner2 : Test {
> T h;
> }
> }
>
> unittest {
> alias Test!(string) StringTest;
> alias Test!(int) IntTest;
>
> auto t1 = new StringTest.Inner1();
> auto t2 = new StringTest.Inner1();
> auto t3 = new IntTest.Inner2();
> auto t4 = new IntTest.Inner2();
> }
>
> int main(string[] args) {
> return 0;
> }
>
> in my real case there are a lot more inner classes (which acutally
> implement the interface defined by the surrounding class).
>
> this is very convenient, because i can create all of the inner classes
> for one type just with an alias. thats what templates are for.
>
> the problem is, i want to pull out the inner classes so that my module
> gets smaller, but then i have to make several aliases to get the desired
> template instances e.g. i would create a module:
> class Innert1(T) : Test!(T) {
> T h;
> }
>
> and an alias for that like: alias Innert1!(string) StringInner1;
>
> that is very repetitive when i have many of those inner classes.
>
> So the question now is. Is there a way to split the big module in
> several smaller ones, and at the same time keep the possibility to let
> the "template magic" do its work?
>
> thanks in advance
>
> christian
Perhaps you can use template mixins (untested).
class Test (T)
{
mixin InnerClasses!(T);
}
template InnerClasses (T)
{
static class Inner1 : Test!(T) {}
// ... and so on
}
--
/Jacob Carlborg
More information about the Digitalmars-d-learn
mailing list