New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unsafe: allow Offsetof for array entries #16740
Comments
See #16727 |
Note that currently Offsetof(a.b.c) tells the offset of c from a.b, not from a. So to be consistent, in your example, Offsetof(T{}.b[1]) should probably be 4 (the offset from T{}.b), not 8 (the offset from T{}). |
A minor language change, would be backward-compatible. |
Another tricky bit: should we require the index to be constant? If not, then there's some evaluation that needs to happen inside the Offsetof(), which is contrary to what Offsetof does now. @mdempsky : I didn't realize that it only applied to the last field. That's surprising. |
So if Offsetof only works on the last offset, then this proposal isn't useful. |
FWIW, I've brainstormed before about extending Offsetof to take two parameters like C++'s offsetof operator. Then you could write Offsetof(a, b.c) vs Offsetof(a.b, c) to distinguish intent. It would be a bit awkward though because then "b" in Offsetof(a, b) doesn't actually refer to a free variable b. Not a problem per se, but some tools might need to be aware of that. It also wouldn't work for array indexes since Offsetof(a, [1]) isn't currently valid syntax, but at least Offsetof(T{}, b[1]) would be. |
It would be nice if
unsafe.Offsetof
could handle array entries as well as struct fields.At the second line of main, I get an invalid expression error:
@griesemer @mdempsky
The text was updated successfully, but these errors were encountered: