Templates, constructors and default arguments

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 24 19:19:26 PST 2014


On Thu, 25 Dec 2014 03:07:55 +0000
aldanor via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> On Thursday, 25 December 2014 at 02:28:47 UTC, ketmar via 
> Digitalmars-d-learn wrote:
> > happy hacking! ;-)
> 
> Thanks once again! I think this mostly solves it. Would it be 
> possible to somehow do the same trick with this()? (I guess due 
> to having to write Type!() when default template arguments are 
> omitted?)

unfortunately, you can have templated constructors, but you can't
instantiate them manually. ;-) i.e.

  class A {
    this(T) () { ... }
  }

can be compiled, but you can't call that constructor. there is simply
no syntax for it. you still can do templated constructors with type
deducing though:

  class A {
    this(T) (T arg) { ... }
  }

  auto n = new A(42); // this will call this!int(42)
  auto n = new A("alice"); // this will call this!string("alice")
  auto n = new A(true); // this will call this!bool(true)

but you can't write:

  auto n = new A!bool(false); // will not compile

the only thing you can do is make constructors private and using
fabric. alas.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141225/a256cb13/attachment-0001.sig>


More information about the Digitalmars-d-learn mailing list