www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5934] New: Finite recursive templates are not allowed

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

           Summary: Finite recursive templates are not allowed
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: maximzms gmail.com



PDT ---
Finite recursion in parametrized structures produces compilation errors.

This kind of recursion could be used to implement custom compile-time
expression parsing for user-defined types.
It should be limited but not forbidden.

Example:
----------
struct Foo
{
    auto opUnary(string op)()
    if(op == "-")
    {
        return Negation!(typeof(this))(this);
    }
}

struct Negation(T)
{
    T statement;

    auto opUnary(string op)()
    if(op == "-")
    {
        return Negation!(typeof(this))(this);
    }
}

void main()
{
    Foo a;
    auto b = -a; // no errors
    auto c = -(-a); /* Error: recursive template expansion for template
                     *        argument Negation!(Foo)
                     */
}
----------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5934




PDT ---
The following code works, but such workarounds are annoying:
----------
struct Foo
{
    auto opUnary(string op)()
    if(op == "-")
    {
        return Negation!(typeof(this))(this);
    }
}

struct Negation(T)
{
    T statement;

    auto opUnary(string op)()
    if(op == "-")
    {
        return MakeNeg(this); // The return type is "Negation!(Negation!(T))"
    }
}

// Function that makes the recursion implicit
auto MakeNeg(T)(T stat)
{
    return Negation!(T)(stat);
}

void main()
{
    Foo a;
    auto b = -a; // no errors
    auto c = -(-a); // no errors
}
----------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5934


Maksim Zholudev <maximzms gmail.com> changed:

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



PDT ---
*** This issue has been marked as a duplicate of issue 3869 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06 2011