4 ms·
You might show an example of how to use D's templates (if it's possible) to emulate the more complex example of computing the bounds from an input argument c
by gavinray 4y ago
You might show an example of how to use D's templates (if it's possible) to emulate the more complex example of computing the bounds from an input argument
char *strncpy(char * restrict dest : count(n),
const char * restrict src : count(n),
size_t n) : bounds(dest, (_Array_ptr<char>)dest + n);
- WalterBright 4y agohttps://news.ycombinator.com/item?id=34085835 https://news.ycombinator.com/item?id=34085835
- gavinray 4y agoI meant something more like this (although this doesn't QUITE compile, but it's close I think): static ubyte* [n*2] requires_ptr_twice_as_large_as_input(n : uint) ( ubyte*[n] input, ubyte*[n*2] output, ) { output[0..n] = input[0..n]; output[n..2*n] = input[0..n]; return output; } void main() { ubyte[10] input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; ubyte[20] output = new ubyte[20]; requires_ptr_twice_as_large_as_input!(10)(&input, &output); }
- ntrel 4y agoThis compiles: ref ubyte[n*2] requires_ptr_twice_as_large_as_input(uint n) ( const ref ubyte[n] input, ref ubyte[n*2] output, ) { output[0..n] = input[0..n]; output[n..2*n] = input[0..n]; return output; } void main() { ubyte[10] input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; ubyte[20] output; requires_ptr_twice_as_large_as_input!(10)(input, output); import std.stdio; writeln(output); assert(output == input ~ input); }
- gavinray 4y agoThank you, neat to see it could be done (I had a hunch!)