static array literal syntax request: auto x=[1,2,3]S;

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 10 19:25:41 PDT 2012


On Monday, June 11, 2012 04:13:42 Mehrdad wrote:
> On Monday, 11 June 2012 at 00:57:45 UTC, Jonathan M Davis wrote:
> > And if all range-based functions were altered to work with
> > static arrays, and we made array literals static, then this
> > example would _still_ be broken
> > auto found = find(arr, [1, 2]);
> 
> Sorry, I don't quite seem to follow. :\
> Would you mind explaining why it's broken?

Because then find would have to slice a static array, and that means that it 
risks returning a slice to a static array, which since the static array was a 
temporary guarantees that you would be returning a slice which pointed to 
garbage. The result is undefined behavior.

Now, since in this particular case, what find returns won't ever include 
anything from any arguments other than the first, it'll be fine. But for any 
templated function which returns a portion of its argument, having it take a 
static array for that argument would be horribly broken. For instance,

auto found = find([1, 2, 3, 4, 5], 3);

_would_ definitely return a slice which referred to garbage were array literals 
static and find accepted them.

- Jonathan M Davis


More information about the Digitalmars-d mailing list