Bug 4816 – template constraint and __traits(compiles, ...) don't work properly together with a delegate
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-09-05T00:42:00Z
Last change time
2012-12-12T22:17:20Z
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-09-05T00:42:07Z
The following refuses to compile
import std.stdio;
void callFunc(alias func, T...)(T args)
if(__traits(compiles, func(args)))
{
func(args);
}
void main()
{
auto func = (){writeln(4);};
callFunc!(func)();
}
Rather, it gives you the error message
d.d(12): Error: template d.callFunc(alias func,T...) if (__traits(compiles,func(args))) does not match any function template declaration
d.d(12): Error: template d.callFunc(alias func,T...) if (__traits(compiles,func(args))) cannot deduce template function from argument types !(func)()
d.d(12): Error: template instance errors instantiating template
If I remove the template constraint, then it compiles fine. If I declare func as a nested function and pass that, it works fine. If I use __traits(compiles, func()) in main(), it returns true. However, the template constraint fails as long as you pass it a delegate.
Comment #1 by dransic — 2010-12-10T15:14:50Z
This code won't compile (D v2.050) either. I think it is the same bug.
import std.stdio;
string process(alias callback)(string s)
if (is(typeof(callback(s))))
{
return s;
}
void main()
{
void fun(string s) { write(s); }
writeln(process!fun("Hello world!")); // OK
enum dlg1 = (string s) { write(s); };
writeln(process!dlg1("Hello world!")); // OK
auto dlg2 = (string s) { write(s); };
writeln(process!dlg2("Hello world!")); // ERROR
}
Error message:
test.d(18): Error: template instance process!(dlg2) does not match template declaration process(alias callback) if (is(typeof(callback(s))))
Comment #2 by k.hara.pg — 2012-12-12T22:17:20Z
Both code in comment#0 and #1 works in 2.061head(2284bb9).