This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Error when forward referencing an enum in a typedef
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Graham Bloice <gbloice at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Sun, 13 Feb 2011 17:04:00 +0000
- Subject: Re: Error when forward referencing an enum in a typedef
- References: <AANLkTim2i05EBsHbab_MRmSFKpONMmOrvxLww-HXLEek@mail.gmail.com>
On 13 February 2011 10:41, Graham Bloice wrote:
> The following code fails to compile on GCC 4.5.1 (CodeSourcery G++
> Lite for ARM), whereas it compiles happily under the ARM compiler and
> Visual Studio 2010.
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> typedef enum abc xyz;
> enum abc {
> ? ?p1 = 0,
> ? ?p2,
> ? ?p3
> };
>
> #ifdef __cplusplus
> }
> #endif
>
> The errors produced are:
>
> test.cpp:5:14: error: use of enum 'abc' without previous declaration
> test.cpp:5:21: error: invalid type in declaration before ';' token
>
> Is this a bug, or stricter compliance?
Stricter compliance, the code is not valid in C++98
> Is there an option to permit
> this sort of construct to compile?
I don't believe so, no.
> I'm aware that putting the typedef
> after the enum definition allows it to compile, but this would then
> mean modifying the vendor supplied headers and keeping track of
> changes would be much more difficult.
C++0x allows an enumeration type to be declared by an
opaque-enum-declaration, which I think would allow you to put a
declaration of the type before the vendor header:
enum abc;
#include "abc.h"
G++ doesn't support this though.