The most confusing error message

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Jan 24 08:01:52 UTC 2018


On Wednesday, January 24, 2018 09:21:09 Shachar Shemesh via Digitalmars-d 
wrote:
> test.d(6): Error: struct test.A(int var = 3) is used as a type
>
> Of course it is. That's how structs are used.
>
> Program causing this:
> struct A(int var = 3) {
>      int a;
> }
>
> void main() {
>      A a;
> }
>
> To resolve, you need to change A into A!(). For some reason I have not
> been able to fathom, default template parameters on structs don't work
> like they do on functions.

It's because in the general case, it would be ambiguous. For instance, what
would this mean?

alias B = A;

Right now, B is an alias of the template A, but if we had implicit
instantiation for types, B would be ambiguous. There are specific cases
where it would not be ambiguous, and arguably, the compiler could be made to
implicitly instantiate the template in those cases, but in general, it
can't, so it never does.

https://issues.dlang.org/show_bug.cgi?id=1012

As it stands, the only time that templates are ever implicitly instantiated
is when a function template is called, because those instantiations are not
ambiguous. A function template with default template arguments isn't
instantiated either if it's not called, because it's the syntax of making
the function call that makes it unambiguous. Hence we talk about IFTI
(Implicit Function Template Instantiation) when we talk about templates
being implicitly instantiated.

- Jonathan M Davis



More information about the Digitalmars-d mailing list