www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2448] New: template return by reference causes seg fault

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

           Summary: template return by reference causes seg fault
           Product: D
           Version: 2.020
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: k-foley onu.edu


import std.stdio;

ref T iif(T)(bool condition, ref T lhs, ref T rhs)
{
        if ( condition ) return lhs;
        return rhs;
}

ref int int_iif(bool condition, ref int lhs, ref int rhs)
{
        if ( condition ) return lhs;
        return rhs;
}

void main()
{
        int a = 10, b = 11;

        int_iif(a<b, a, b) = 42;
        writeln("a = ", a, " and b = ", b); // a = 42 and b = 11

        iif(a<b, a, b) = 52; // seg fault
        writeln("a = ", a, " and b = ", b);
}


-- 
Nov 11 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2448


andrei metalanguage.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major





Adding another example and bumping this to major because it stops std.algorithm
development dead in its tracks.

template ElementType(R)
{
    alias typeof({ R r; return r[0]; }()) ElementType;
}

bool empty(T)(in T[] a) { return !a.length; }
void next(T)(ref T[] a) { assert(a.length); a = a[1 .. $]; }
void retreat(T)(ref T[] a) { assert(a.length); a = a[0 .. $ - 1]; }
T head(T)(T[] a) { assert(a.length); return a[0]; }
ref T toe(T)(T[] a) { assert(a.length); return a[a.length - 1]; }

struct Retro(R)
{
private:
    R _input;

public:
    bool empty()
    {
        return _input.empty;
    }

    void next()
    {
        _input.retreat;
    }

    ElementType!(R) head()
    {
        return _input.toe;
    }
}

void main()
{
    void test(int[] input)
    {
        foreach (e; Retro!(int[])(input))
        {
        }
    }
    test([ 1 ]);
}


-- 
Jan 25 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2448


bugzilla digitalmars.com changed:

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





Fixed dmd 2.025


-- 
Mar 11 2009