Bug 9179 – Invalid template instantiation attempt should result in a readable error message
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-12-18T09:44:19Z
Last change time
2020-05-25T08:01:58Z
Keywords
diagnostic
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2012-12-18T09:44:19Z
template Pred(T)
{
enum Pred = true;
}
void func(T)(T t)
if (Pred(T))
{
}
void main()
{
func(1);
}
Errors:
test.d(9): Error: template test.Pred does not match any function template declaration. Candidates are:
test.d(3): test.Pred(T)
test.d(9): Error: template test.Pred(T) cannot deduce template function from argument types !()(int)
test.d(15): Error: template test.func does not match any function template declaration. Candidates are:
test.d(8): test.func(T)(T t) if (Pred(T))
test.d(15): Error: template test.func(T)(T t) if (Pred(T)) cannot deduce template function from argument types !()(int)
The problem is Pred was used as a function instead of as a template, the fix is:
void func(T)(T t)
if (Pred!(T)) // note the !() syntax
{
}
There is no other way the Pred template can be called because it's not a function template, therefore we should print out a better error message such as:
test.d(9): Error: Need to use !() syntax to instantiate template test.Pred
Comment #1 by andrej.mitrovich — 2013-11-05T13:21:53Z
*** Issue 11444 has been marked as a duplicate of this issue. ***
Comment #2 by b2.temp — 2020-05-25T08:01:58Z
This particular case is well diagnosized nowadays:
>/tmp/temp_7FAAE5E75B70.d(7,14): Error: cannot pass type `int` as a function argument
>/tmp/temp_7FAAE5E75B70.d(13,9): Error: template `temp_7FAAE5E75B70.func` cannot > deduce function from argument types `!()(int)`, candidates are:
>/tmp/temp_7FAAE5E75B70.d(6,6): `func(T)(T t)`