Remix.run Logo
uecker 6 hours ago

Or just do it in C.

  #define span(T) struct span_##T { size_t len; T *data; }
  #define span_access(T, x, i) (*({              \
    span(T) *_v = (x);                           \
    auto _i = (i);                               \
    if (((size_t)_i) >= _v->len) abort();        \
    &_v->data[_i];                               \
  }))
https://godbolt.org/z/TvxseshGc
nananana9 5 hours ago | parent | next [-]

Still requires a gcc/clang specific extension (although this one I'd be very happy to see standardized)

uecker 3 hours ago | parent [-]

Only statement expressions, but one can also implement this without them.

fuhsnn 4 hours ago | parent | prev [-]

The fact that pointer types can't be used with this pattern without typedef still seems kinda primitive to me.

uecker 3 hours ago | parent [-]

You can use pointer types by using a typedef first, but I agree this not nice (I hope we will fix this in future C). But then, I think this is a minor inconvenience for having an otherwise working span type in C.