Template argument deduction and default args

Manu via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 23 21:53:21 PDT 2014


I'm running into consistent problems with default args and argument
deduction in templates.
There seem to be 2 consistent classes of problem:

struct S(size_t len = 10)
{
  ubyte[len] data;
}

S!100 x; // this works fine
S y; // this doesn't work (!)
S!() z; // this works

The template arg has a default arg, why require !() ??
This causes problems in meta code, where you want to create an instance of
some T, and T may be a normal type with no template args, in which case !()
is invalid, but a template type with default args should also be
acceptable, but it doesn't work because the meta code doesn't specify !().


The other case I am running in to is when I have 'struct S(T)' or 'class
C(T)', where T can be inferred from the constructor, but it isn't.

struct S(T)
{
  this(T t)
  {
    m = t;
  }

  T m;
}

int myThing;
auto s = S(myThing); // error!
auto s = S!(typeof(myThing))(myThing); // works, but horrible, and breaks
down again when used in place of non-template counterparts in meta code.


My main gripe here is that in both these cases which require special
treatment for basic instantiation, they break down when used in meta code
without loads of hacks and ugly mess throughout the meta to handle these
cases.

Why these restrictions? Can these cases be fixed?
I have quite a lot of code that would be sanitised by these changes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140724/a337b1d1/attachment.html>


More information about the Digitalmars-d mailing list