Skip to content
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

spec: document that Alignof, Offsetof, and Sizeof do not evaluate their arguments #12946

Open
mdempsky opened this issue Oct 15, 2015 · 2 comments
Assignees
Milestone

Comments

@mdempsky
Copy link
Member

At least in cmd/compile, unsafe.Alignof, unsafe.Offsetof, and unsafe.Sizeof do not evaluate their arguments: http://play.golang.org/p/4QE3mVrFaS

Although unsurprising and consistent with C/C++, the Go spec doesn't mention this. It does say

Calls to Alignof, Offsetof, and Sizeof are compile-time constant expressions of type uintptr.

but I don't think that's necessarily mutually exclusive with evaluating the argument expression.

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 15, 2015
@gopherbot
Copy link

CL https://golang.org/cl/16004 mentions this issue.

@tzneal
Copy link
Member

tzneal commented Aug 11, 2018

I like the current behavior, as it allows the following to work which lets you get sizes of arbitrary types given their names:

unsafe.Sizeof(*(*uint8)(nil))

See https://play.golang.org/p/MeFfDzoGpxo, my reading of "compile-time constant expressions of" implied that is wouldn't evaluate the expression, leading me to the above construct. I'm using this in some generated code now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants