Question Details

No question body available.

Tags

c++ c

Answers (2)

Accepted Answer Available
Accepted Answer
April 18, 2025 Score: 4 Rep: 18,777 Quality: High Completeness: 50%

In C, the nested struct definitions all occupy global namespace

Hence, in C, the definitions need not be nested. Nesting the definitions does show a logical connection, but this connection is already shown with the naming scheme (in that longprojectnamexyz represents z in y in x in longprojectname). In C++, the benefits of nesting the definitions are neither used nor desired. So, in the absence of other considerations, one solution could be to define the types in sequence rather than nesting them.

struct longprojectnamexyz {
    int i;
};

struct longprojectnamexy { struct longprojectnamexyz z; };

struct long
projectnamex { struct longprojectnamexy y; };
April 18, 2025 Score: 7 Rep: 229,222 Quality: Medium Completeness: 50%

But that fails to compile when prefixed with struct tag.

Don't prefix them with struct, so

#ifdef cplusplus
using longprojectnamexy = longprojectnamex::longprojectnamexy;
using longprojectnamexyz = longprojectnamexy::longprojectnamexyz;
#else
typedef struct longprojectnamex longprojectnamex;
typedef struct longprojectnamexy longprojectnamexy;
typedef struct longprojectnamexyz longprojectnamexyz;
#endif

long
projectnamex x = { 1 }; longprojectnamexy y = { 1 }; longprojectnamexy_z z = { 1 };