Here’s a minimal example of what I’m trying to do:
enum color { RED, GREEN, BLUE };
typedef enum legacy_color enum color;
int
main(void)
{
return 0;
}
Compilation fails with
test.c:13:27: error: two or more data types in declaration specifiers
13 | typedef enum legacy_color enum color;
| ^~~~
test.c:13:32: warning: empty declaration with storage class specifier does not redeclare tag
13 | typedef enum legacy_color enum color;
| ^~~~~
Motivation: I need to rename an enum to align with current naming conventions in my project. The old name needs to remain as an alias, which will be declared in a compatibility header file. Referring back to my example, enum color { RED, GREEN, BLUE }
will be in the main header file, and typedef enum legacy_color enum color
would go in the backward-compatibility header file.
The only other approach I’ve been able to come up with is #define legacy_color color
, but that’s overly broad and would match outside the enum type context.
Note that typedef enum color legacy_color;
is not acceptable, because the type has changed from enum legacy_color
to simply legacy_color
.
You have the syntax of typedef
backwards. The name being defined comes at the end. You also don’t repeat enum
. Use
typedef enum color legacy_color;
This will make legacy_color
a synonym for the enum you defined on the previous line.
enum color { RED, GREEN, BLUE };
typedef enum color legacy_color;
legacy_color foo = GREEN;
enum color bar = BLUE;
int
main(void)
{
return 0;
}