Formal review of std.buffer.scopebuffer

monarch_dodra monarchdodra at gmail.com
Mon Mar 17 13:25:10 PDT 2014


On Monday, 17 March 2014 at 20:06:49 UTC, Walter Bright wrote:
> On 3/17/2014 12:03 PM, monarch_dodra wrote:
>> So yes, it compiles, but the implementation is still wrong. 
>> Your implementation
>> allows assigning an immutable(int)* into an int*.
>>
>> Unless there is a performance specific reason for using the 
>> const, I don't see
>> why we aren't removing it.
>
> The reason was I was alternately calling put() with mutable 
> char[] and immutable strings. I should look and see if there's 
> a better way.

Ah... right. That makes sense. Templatising it is what usually 
makes the most sense, and most easilly solves the problem. 
Something like:
put(E)(E[] s)
if (is(typeof(buf[] = s)))

That said, given that:
1: You might be worrying about un-needed template bloat for types 
without indirections (such as strings)
2: You are only working with mutable types.

Then a static if should do the trick. Something like:
//----
static if (is(const(T) : T)
     alias CT = const(T);
else
     alias CT = T;

void put(T[] s)
{
     ...
     buf[used .. newlen] = s[];
}
//----
unittest
{
     ScopeBuffer!(int*) b;
     int*[] s;
     b.put(s);

     ScopeBuffer!char c;
     string s1;
     char[] s2;
     c.put(s1);
     c.put(s2);
}
/----

This works for me.

Does this change seem acceptable for you? Should I submit that?


More information about the Digitalmars-d mailing list