For example, I want something similar in meaning to this:
//Somethere in Windows header
struct COUPLE {
WORD part0;
WORD part1;
}
//In my code
#if sizeof(COUPLE) == sizeof(INT)
#define COUPLE_TO_INT(arg) (*((INT*)((void*)&arg)))
#else
inline INT COUPLE_TO_INT(const COUPLE &arg) {
return ((INT)arg.part1 << 16) + arg.part0;
}
#endif
Of course, the code from the example is not compiled.
And, of course, I can do with just the INT COUPLE_TO_INT(const COUPLE &arg)
function, but as I noticed, in most cases it is not required and I can do with reinterpret_cast
, which requires less resources (shifting and summation). However, there may be situations where padding breaks this mechanism, so a backup path is required.
It is clear that I cannot influence the alignment of the structures from the header in any way, but I can find out their size and act on this.
Is it possible to branch a macro based on an C++ assert or something of the same kind?