-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cmd/cgo: const identifier is non-constant in Go code #19816
Comments
This is correct behaviour. C does not allow a |
Indeed, I'm currently working on C++ code with only a C binding. const int SIZE = sizeof(double);
typedef int Array[SIZE];
int main(int argc, char *argv[])
{
Array array;
array[0] = 0;
return 0;
} However, the following Go code will compile and run just fine : package main
import "unsafe"
const SIZE = unsafe.Sizeof(float64(0))
type Array [SIZE]byte
func main() {
var a Array
a[0] = 0
} Then, I just do not understand why the code I put in my first post will not compile. I can turn this bug report into a proposal if this is "working as intended" in Go. |
I don't think @ianlancetaylor intends for cgo to support C++, but I'll let him decide. |
It would be nice if cgo could treat C declarations of the form
as equivalent to Go statements of the form
for types That seems more-or-less independent of the C++ support for the same usage. |
@bcmills totally got my point. C++ support for However, fixing (if it's a bug) or implementing (if it's a proposal, as it's not completely clear yet) |
I don't think this is a good idea. The corresponding C code
does not compile. Treating a C For the reason, it's also hard to implement. The cgo tool tries to figure out what kind of name it is given by generating C code and looking at the errors it gets. Specifically it will generate code like
An error in It would be possible in principle to special case it. The debug info for Once we've detected that it is immutable, we would have to determine the value, since a Go constant can not refer to the C value. cgo normally determines the value of a constant by generating code like
and then examining the value of the variable. But that again does not work in C (it does in C++), so again we would need to special case determining the value. So it's special cases at every level. It doesn't seem worth doing to me. Sorry. |
When you don't know how I'm working on a corner case implying Go <=> C <=> C++ which leaded to this Have a nice day. |
What did you do?
What did you expect to see?
go build
terminating without error, which is the case when I use the first definition (commented here) ofArray
What did you see instead?
./main.go:9: non-constant array bound *_Cvar_CONST2
System details
The text was updated successfully, but these errors were encountered: