Bug 2448 – template return by reference causes seg fault

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-11-11T12:06:00Z
Last change time
2015-06-09T01:20:25Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
k-foley

Comments

Comment #0 by k-foley — 2008-11-11T12:06:36Z
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); }
Comment #1 by andrei — 2009-01-25T23:32:28Z
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 ]); }
Comment #2 by bugzilla — 2009-03-11T14:50:18Z
Fixed dmd 2.025