Macro to add a suffix to an unsigned long long literal

I work with a library that defines a constant like this:

#define SOME_BIG_CONSTANT 0x0000000100000000 

This literal is too large to be represented as long , so any program that uses this macro cannot compile (using gcc 4.1.2 for VxWorks). The solution (non-standard, but supported by this compiler) is to add the ull suffix to the literal:

 #define SOME_BIG_CONSTANT 0x0000000100000000ull 

However, this will require me to change the title of the library, which I would rather not do. I suck macros, so my question is how can I define a macro that will add this suffix, which I could name as follows:

 ULL_(SOME_BIG_CONSTANT) 

What will expand to:

 0x0000000100000000ull 
+4
source share
2 answers

ull is the standard suffix in C ++ 11.

Alternatively, you can define the following macros:

 #define APPEND(x, y) x ## y #define ULL(x) APPEND(x, ull) 

Now you can use:

 int main() { unsigned long long a = ULL(SOME_BIG_CONSTANT); return 0; } 
+8
source
 #define ULL_2(NUM) NUM ## ull #define ULL_(NUM) ULL_2(NUM) 

Must do the job. (Note: untested)

The second macro is needed to trigger the macro distribution in the passed macro.

+3
source

Source: https://habr.com/ru/post/1387565/


All Articles