Currently I have a program which need attribute packing for numerous functions. The code must be interchangeable between Windows Visual Studio C++ and GCC C++. Currently it is very hard to read and I wanted to clean it up with a C++ macro, however, I noticed that after I use the macro once, the next lines of code exit the current namespace. Is there a way to fix this? Here is sample code similar to what I am working on and images of the problem.
#if defined(_WIN32) || defined(_WIN64)
#define PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop))
#else
#define PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#endif
namespace test {
PACK(typedef struct temp1 {
int a;
int b;
});
PACK(typedef struct temp2 {
int c;
int d;
});
}
In visual studio, when my cursor is in the 1st struct, VS shows it is within the namespace test, however, the 2nd in the global namespace. Any ideas on what would fix this?
As of now my structs look more like the code below, which works, but it makes it much harder to read especially with 100+ of these packed structs.
#define PACK __attribute__((__packed__))
namespace test {
#if defined(_WIN32) || defined(_WIN64)
#pragma pack(push, 1)
#endif
typedef struct temp1 {
int a;
int b;
}
#if defined(_WIN32) || defined(_WIN64)
temp2;
#pragma pack(pop)
#else
PACK temp2;
#endif
#if defined(_WIN32) || defined(_WIN64)
#pragma pack(push, 1)
#endif
typedef struct temp2 {
int c;
int d;
}
#if defined(_WIN32) || defined(_WIN64)
temp2;
#pragma pack(pop)
#else
PACK temp2;
#endif
}


There is no need to
typedefastructdeclaration in C++. That is only needed in C, if you don't want to use thestructkeyword every place a struct type is used (variables, function parameters, etc).In Visual Studio, it would be easier to just put a single
#pragma pack(push, 1)before the firststructdeclaration, and a single#pragma pack(pop)after the laststructdeclaration. You don't need to pack the structs individually.On GCC, you can skip the
#pragmaand just use__attribute__((packed))on eachstructdeclaration.Try something more like this: