Instantiating a class with range template parameter
Jon Degenhardt via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 8 02:03:51 PDT 2016
On Thursday, 8 September 2016 at 08:44:54 UTC, Lodovico Giaretta
wrote:
> On Thursday, 8 September 2016 at 08:20:49 UTC, Jon Degenhardt
> wrote:
>>
>> [snip]
>>
>
> I think that
>
> auto x = new Derived!(typeof(stdout.lockingTextWriter()))(); //
> note the parenthesis
>
> should work.
>
>
>
> But usually, you save the writer inside the object and make a
> free function called `derived` (same as the class, but with
> lowercase first). You define it this way:
>
> auto derived(OutputRange)(auto ref OutputRange writer)
> {
> auto result = new Derived!OutputRange();
> result.writer = writer; // save the writer in a field of
> the object
> return result;
> }
>
> void main()
> {
> auto x = derived(stdout.lockingTextWriter);
> x.writeString("Hello world"); // the writer is saved in
> the object, no need to pass it
> }
Yes, the form you suggested works, thanks! And thanks for the
class structuring suggestion, it has some nice properties.
More information about the Digitalmars-d-learn
mailing list