Nested structures in headers and C++ compatibility?

Peter Dufault dufault at hda.com
Thu Oct 16 10:42:47 UTC 2014


In the MPC55XX headers there are typedefs that reference nested structures such as:

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

typedef struct {
  uint32_t index : 10;
  uint32_t count : 10;
  uint32_t output : 1;
  union SIU_PCR_tag pcr;
} mpc55xx_siu_pcr_config;

#ifdef __cplusplus
}
#endif

where SIU_PCR_tag is a nested structure:

struct SIU_tag {
   ...
   union SIU_PCR_tag {     /* Pad Configuration Registers */
      ...
   }
};

and extern "C" brackets don't prevent C++ errors such as:

error: field 'pcr' has incomplete type
   union SIU_PCR_tag pcr;

The only hack I have is to re-define the typedef to scope the type:

typedef struct {
  uint32_t index : 10;
  uint32_t count : 10;
  uint32_t output : 1;
#ifdef __cplusplus
  union SIU_tag::SIU_PCR_tag pcr;
#else
  union SIU_PCR_tag pcr;
#endif
} mpc55xx_siu_pcr_config;

although I suppose a preferable approach might be:

#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag_type;
#else
typedef union SIU_PCR_tag SIU_PCR_tag_type;
#endif
...
typedef struct {
  uint32_t index : 10;
  uint32_t count : 10;
  uint32_t output : 1;
  SIU_PCR_tag_type pcr;
} mpc55xx_siu_pcr_config;

Any thoughts?  Any better way?

Peter
-----------------
Peter Dufault
HD Associates, Inc.      Software and System Engineering



More information about the users mailing list