structure tags and identifier name spaces (was: [cairo] C++ bindings)

Carl Worth cworth at cworth.org
Thu Jan 13 07:05:57 PST 2005


On Sat, 01 Jan 2005 16:19:24 -0500, Jonathan Brandmeyer wrote:
> I've been working on a C++ wrapper for Cairo.  Two issues have come up
> so far.  First, in cairo.h, "cairo" is used as an incomplete type.  This
> makes it difficult to create the highly desirable namespace "cairo".

Oh, ick. Of all the annoying things that C++ breaks...

In C, after including cairo.h, "cairo" is still an undeclared
identifier, not an incomplete type, (while "struct cairo" is an
incomplete type). The specification clearly states that structure tags
exist in a separate name space from ordinary identifiers.

Not so in C++.

So, the following program contains a syntax error in C, but is a valid
C++ program:

	struct foo { int val; };

	int main (void) {
	    foo f = {42};
	    return f.val;
	}

As confirmed by the implementation in gcc/g++:

	$ gcc structtest.c && echo success
	structtest.c: In function `main':
	structtest.c:4: error: `foo' undeclared (first use in this function)
	structtest.c:4: error: (Each undeclared identifier is reported only once
	structtest.c:4: error: for each function it appears in.)
	structtest.c:4: error: syntax error before "f"

	$ g++ structtest.c && echo success
	success

I hadn't realized that C++ breaks C this way. That's annoying. But
I've gone ahead and renamed "struct cairo*" to "struct _cairo*"
throughout the implementation anyway.

-Carl

PS. I'll save issue #2 for another message.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/cairo/attachments/20050113/bb2cf232/attachment.pgp


More information about the cairo mailing list