template auto instantiation when parameters empty

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 8 01:34:03 PDT 2016


On Thursday, May 05, 2016 19:09:01 Steven Schveighoffer via Digitalmars-d-
learn wrote:
> On 5/5/16 6:50 PM, Erik Smith wrote:
> > Alias works at the cost of adding a 2nd type name:
> >
> > alias Res = Resource!();
> > auto res  = Res.create
> >
> > The other problem is that the alias definition by itself instantiates,
> > which I can't afford.
> >
> > I have createResource() now, it just doesn't fit well with the rest of
> > the calling styles.
> >
> > There is also this approach, which might be slightly more idiomatic
> >
> > struct Resource(T) {}
> > struct Policy {}
> >
> > auto create(alias T)() {
> >
> >      T!Policy r;
> >      return r;
> >
> > }
> >
> > auto r = create!Resource;
>
> I believe factory external IFTI functions are quite idiomatic. They are
> all over phobos.

When dealing with the creation of objects that are templatized, they're
often practically required for using the object to be sane - particularly
when the constructor itself needs to be further templatized. And since IFTI
only works with functions, if you want something similar with templated
types, wrapping the construction in a function to get IFTI is pretty much
your only option if you want to avoid requiring explicit instantiations.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list