Hacker News new | ask | show | jobs
by uecker 371 days ago
Something like this: https://godbolt.org/z/er9n6ToGP It encapsulates a pointer to an array and a length. It is not perfect because of some language limitation (which I hope we can remove), but also not to bad. One limitation is that you need to pass it a typedef name instead of any type, i.e. you may need a typedef first. But this is not terrible.
1 comments

Thanks, this is great! I've been having a look at your noplate repo, I really like what you're doing there (though I need a minute trying to figure out the more arcane macros!)
In this case, the generic span type is just #define span(T) struct CONCAT(span_, T) { ssize_t N; T* data; } And the array to span macro would just create such an object form an array by storing the length of the array and the address of the first element. #define array2span(T, x) ({ auto __y = &(x); (span(T)){ array_lengthof(__y), &(__y)[0] }; })