www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4816] New: template constraint and __traits(compiles, ...) don't work properly together with a delegate

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4816

           Summary: template constraint and __traits(compiles, ...) don't
                    work properly together with a delegate
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmail.com



00:42:07 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 05 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4816


Nicolas Sicard <dransic free.fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dransic free.fr



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))))

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4816


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME





-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 12 2012