Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro for appending suffix to unsigned long long literal

Tags:

c++

macros

I am working 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 fails to compile (using gcc 4.1.2 for VxWorks). The (non-standard, but supported by this compiler) solution that works is to add the suffix ull to the literal:

#define SOME_BIG_CONSTANT 0x0000000100000000ull

However, that would require me to modify the library-header, which I'd rather not do. I suck at macros, so my question is, how can I define a macro that would add that suffix, which I could call like this:

ULL_(SOME_BIG_CONSTANT)

Which would expand to:

0x0000000100000000ull
like image 290
Björn Pollex Avatar asked Dec 22 '11 13:12

Björn Pollex


1 Answers

ull is a standard suffix on C++11.

On the other hand, you may 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;
} 
like image 91
J. Calleja Avatar answered Sep 23 '22 02:09

J. Calleja