C: macro to check A < B < C at compile time using static assert?

Viewed 41

I've got some macros:

#define D(x)        (((uint64_t) 1) << (x))

#define DATA0           D(0)
#define DATA1           D(1)
#define DATA2           D(2)
#define DATA3           D(3)
#define DATA4           D(4)
#define DATA5           D(5)

#define COMB1           DATA1 | DATA4 | DATA5
#define COMB2           DATA0 | DATA2 | DATA4
#define COMB3           DATA2 | DATA3
#define COMB4           DATA2 | DATA3 | DATA1

One important thing is that COMBs need to have data ordered in such a way that dataA < dataB < dataC < ... < dataN, so in the above cases all are OK except COMB4.

Would it be possible to do a static assert and check this at compile time? For COMB1 (DATA1 < DATA4 < DATA5), for COMB2 (DATA0 < DATA2 < DATA4), and finally fail for COMB4 because DATA3 > DATA1?

0 Answers
Related