Skip to content

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

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

Open
mdempsky opened this issue Oct 15, 2015 · 2 comments
Assignees
Labels
Documentation Issues describing a change to documentation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mdempsky
Copy link
Contributor

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
Contributor

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.

@griesemer griesemer modified the milestones: Unplanned, Go1.19, Backlog May 12, 2022
@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants