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
Still requires a gcc/clang specific extension (although this one I'd be very happy to see standardized)
Only statement expressions, but one can also implement this without them.
Still, please standardize them :).
I don't use much C but if you add them to the standard they'll probably trickle down to C++ compilers by 2045 and I'll have a good 10 years to use them before I retire.
You are not happy with immediately invoked lambda expressions?
Well, I'll take them over the nothing you're giving me :D
But in all seriousness, I want this:
Can't be done with lambdas since the macro needs to return out of the actual function, not the lambda. Pretty much rust question mark, but without rust, or zig "try" but without zig.I see, thanks! There is general consensus that statement expressions should become part of ISO C, but some lack of time to get it done. I am not part of WG21 though, so can't say anything about C++.
The fact that pointer types can't be used with this pattern without typedef still seems kinda primitive to me.
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.