Nested structures in headers and C++ compatibility?

Gedare Bloom gedare at rtems.org
Thu Oct 16 14:18:53 UTC 2014


On Thu, Oct 16, 2014 at 6:42 AM, Peter Dufault <dufault at hda.com> wrote:
> 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;
>
Maybe introduce a new conditional typedef would work, something like..

#ifdef __cplusplus
typedef SIU_tag::SIU_PCR_tag SIU_PCR_tag
#endif

Then the structure definition can stay the same.

I haven't seen this before so I don't know if it would work, but it
makes sense to me.

-Gedare

> 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
>
> _______________________________________________
> users mailing list
> users at rtems.org
> http://lists.rtems.org/mailman/listinfo/users



More information about the users mailing list