Search code examples
cenumstypedeffreeimage

Typedef and enum with same name?


In the library FreeImagePlus, in FreeImage.h, there is a funny #define which seems to create a typedef and an enum with the same name:

#define FI_ENUM(x)      typedef int x; enum x

This is expanded by the preprocessor to code like:

typedef int FREE_IMAGE_FILTER;
enum FREE_IMAGE_FILTER {
 FILTER_BOX = 0,
 FILTER_BICUBIC = 1,
[...]

What does this do? Is it even legal to have a typedef and an enum with the same name? And isn't an enum compatible to int anyway? Why does FreeImage do this?


Solution

  • Names of structures, unions and enumerations lives in their own namespace. That's why you can declare a struct/union/enum variable with the same name as the actual struct/union/enum.

    And it's not the name of the complete enum (e.g. for enum X I mean the X) that has to be compatible with an integer, it's the names inside the enumeration.