Albert Hopkins <mar...@letterboxes.org> writes:

> I have the following code snippet:
>
> typedef volatile struct {
> } mystruct;
>
> void mytest(mystruct* x) {};
>
> As a C program (gcc) this compiles fine, but with g++ I get the
> following error:
>
> test.cpp:4: error: non-local function ‘void mytest(volatile mystruct*)’
> uses anonymous type
> test.cpp:2: error: ‘typedef volatile struct<anonymous> mystruct’ does
> not refer to the unqualified type, so it is not used for linkage
>
> So my question is: is g++ reporting the error incorrectly, or is it not
> valid c++ and if so why?

This question would be more appropriate for the gcc-h...@gcc.gnu.org
mailing list.  Please take any followups to that mailing list.

In C++, an externally visible function can not refer to a local type.
The reason is that there is no way for a function defined in another
file to call it, because that function can not create an instance of the
local type.  In this case, the local type is the anonymous struct.

It may seems that a function in another file could use the type
mystruct.  That would be permitted if you wrote "typedef struct {}
mystruct", because that would effectively mean that mystruct was the
name of the struct.  However, you used the volatile qualifier, and that
applies to the typedef, not to the struct.  That is, mystruct is a
volatile version of a struct which can not be named.  No function in
another file is going to be able to create an instance of that struct,
so the function mytest is not permitted to have external linkage.

Ian

Reply via email to