Template parameter defaults
Steven Schveighoffer
schveiguy at yahoo.com
Tue May 31 10:49:24 PDT 2011
On Mon, 30 May 2011 09:42:53 -0400, Johann MacDonagh
<johann.macdonagh..no at spam..gmail.com> wrote:
> I'm wondering if there's a cleaner way to do this:
>
> class Test(T = uint)
> {
> this(string s)
> {
> }
> }
>
> void main(string[] argv)
> {
> auto a = new Test!()("test");
> }
>
> I'd *like* to be able to do this:
>
> auto a = new Test("test");
>
> and:
>
> auto a = new Test!double("test");
>
> The only possibility I see is to do this:
>
> alias Test!() Test2;
>
> But that introduces two types a user has to decide between. Any ideas?
> Am I out of luck here?
Currently, you can omit the template args only in the case of IFTI
(Implicit Function Template Instantiation) which actually deduces your
template arguments based on the function call.
I'd argue actually, that IFTI should be extended to constructors:
class Test(T)
{
this(T t) {}
}
T t;
auto a = new Test(1);
static assert(is(typeof(a) == Test!int));
Which would also cover your case.
This should be a no-brainer since a constructor call is almost identical
in nature to a function call. For sure the overload resolution is the
same.
I thought there was a bugzilla entry for this, but I couldn't find it with
some simple searches, anyone know of one? If not, I'll file one.
-Steve
More information about the Digitalmars-d-learn
mailing list