This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Is this valid c++ ?
- From: Ian Lance Taylor <iant at google dot com>
- To: Albert Hopkins <marduk at letterboxes dot org>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 30 Apr 2009 14:54:23 -0700
- Subject: Re: Is this valid c++ ?
- References: <1241120614.28337.24.camel@localhost.localdomain>
Albert Hopkins <marduk@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-help@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