How to chain constructor args to a template memeber

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 11 10:57:44 PST 2010


On Thu, 11 Mar 2010 02:58:52 -0500, BCS <none at anon.com> wrote:

> Using D2, I have a template class that looks something like this:
>
> class C(T) { T t; }
>
> (For simplicity, assume T is required to be a struct of some kind.) I  
> want to have a constructor that passes on some of it's args to a  
> constructor for t. This is easy as long as I don't need it to work for  
> arbitrary constructors.
>
> What I want to do (that doesn't seem to work) is this:
>
> this(Args...)(int foo, float bar, Args args)
> {
>    t = T(args);
>
>    ...
> }
>
> Any ideas on fixes or work arounds?

What about a static function instead of a constructor?

i.e.

static C create(Args...)(int foo, float bar, Args args)
{
    auto c = new C(foo, bar);
    c.t = T(args);
    return c;
}

It's a shame template constructors aren't allowed, they aren't even  
virtual functions!

-Steve


More information about the Digitalmars-d-learn mailing list