https://www.digitalmars.com/articles/b44.html
It's simple and it works, we've got 15 years experience with this in D.
https://www.digitalmars.com/articles/b44.html
It's simple and it works, we've got 15 years experience with this in D.
[1]: https://www.comp.nus.edu.sg/~gregory/papers/ndss17stack.pdf
It's purely additive, which means you can incrementally adopt it in places where it matters.
f(int *a, int n) -> f(int *count(n) a, int n);
or struct { int len; int *values; }
-> struct { int len; int *count(len) values; }
And other annotations for unions, etc.This actually works quite well in practice - the cited paper involved us applying these annotations to a complete, bootable linux kernel. You do have to be willing to tolerate limited code changes and a few "trust me" casts
Disclaimer: I worked on the overall project, though not significantly on the Deputy part.
int s[3];
int *p;
f(s, sizeof(s)/sizeof(s[0]));
f(p, n);
f(int *count(n) a, int n);
(which has a mistake, n should be size_t not int), or: int s[3];
int* p;
int[] a;
f(p[0..n]); // convert pointer to phat pointer
f(a); // or use existing phat pointer
f(s); // static arrays just work
f(int[] a); // the declaration of f() uint16_t count;
uint16_t *COUNT(count) buffer;
uint16_t *BND(buffer, buffer + count) pos; // pos is within the specified bounds
which captures that buffer is a known-size array, and that pos ranges within buffer's contents. Of course, writing from scratch, one might prefer uint16_t[] buffer;
uint16_t pos; // An offset within buffer
but that's not much help when incrementally modifying an existing code base, or interacting with some external library one either doesn't have source for or doesn't wish to modify.I am doing exactly that now that I converted the Digital Mars C++ and the DMD D programming language compilers to D - converting the pointer based array code to [] based arrays. Doing it incrementally is very doable and easy.
It's not viral like const is. A slice can be trivially created:
a[] = p[0 .. length];
and the other way: p = a.ptr;
length = a.length;FWIW, you will see a class called StringPiece copied all over a lot of open source Google projects, including Chrome, Ninja, RE2, probably Android, etc.
That is essentially a pointer-length pair. I agree it would be nice to have a dedicated syntax for it, and not a class that everybody has to copy around.
C++ 17 names this string_view (long overdue!)
I just Googled and apparently there was a problem with array_view ? That's a shame.
https://www.reddit.com/r/cpp/comments/5ya5pe/whatever_happen...
The optimizer can convert a library type to a builtin type if it still behaves as specified in the standard.
template<size_t N>
void foo(int (&array)[N]) {}For static arrays, no, the compiler does it. For turning pointers into phat pointers, yes, the user has to supply the length. But he only has to do that once.
> exactly the same
It isn't, for the reasons:
1. The compiler knows about it, so it can be used for optimization purposes, and to optionally insert bounds checking or not.
2. Consider why C has structs. It's because it's a lot less error prone to group related values together, rather than having a collection of independent variables. It makes the code easier to read, too.
1. phat pointers for arrays
2. nested functions
(Nested functions have a surprising ability to untangle ugly control flow, including getting rid of most uses of goto's.)
Why is that exactly?