1

Imagine this:

#define PUTVALUE 0x000000000000000F
#define SetStr(s) literate(s)
#define literate(s) #s

...

foo (PUTVALUE, SetStr(PUTVALUE));

How can I make work this, where foo wants as first parameter a 64 bit integer, and as second parameter a const string with a hexadecimal representation of that integer with leading 0x.

So I cant do:

#define PUTVALUE 0x000000000000000Full

as this would break the second parameter.

But not doing so is breaking the first.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
dhein
  • 6,431
  • 4
  • 42
  • 74
  • 1
    What's the problem with the code you've shown? `0x0F` will be implicitly cast to a 64-bit int if you pass it to a function that expects one. – interjay Sep 10 '14 at 13:23
  • @interjay Because the function parameters are part of a variable argument list, and the types are just my internal rule. This is just the minimal example. – dhein Sep 10 '14 at 13:28
  • Do you want a constant or a macro? – David Heffernan Sep 10 '14 at 13:39
  • Your question title says "constant", but I cannot see any constants, only macros. And your response to @interjay's comment indicates that the question is missing vital information. – David Heffernan Sep 10 '14 at 13:41
  • What exactly is the problem with my Question? hm? I explained whats the problem in an unique way. And I also explained why it is neccsesary to specify it explicitly as a 64bit "macro". – dhein Sep 10 '14 at 14:01
  • The "function parameters are part of a variable argument list" and the post's "foo wants as first param a 64 bit integer" is a contradiction as variable argument lists are never the first parameter. @interjay point is correct in that a function expecting a 64-bit would perform an implicitly cast. The post and self-answer are good but need refinement. – chux - Reinstate Monica Sep 10 '14 at 14:33
  • @chux I also wrote that this is just to simplyfy my damn situation. ofc it isnt the first argument, in my case there are additional 3 strings before the varglist begins. but why should this interesting you?..... – dhein Sep 10 '14 at 14:51

1 Answers1

1

Oh I figgured just out an awesome way to do so:

foo (INT64_C(PUTVALUE), SetStr(PUTVALUE));

Is doing exactly that job.

dhein
  • 6,431
  • 4
  • 42
  • 74
  • Disagree about "doing _exactly_ that job". `INT64_C()` expands an integer constant to type `int_least64_t`, not `int64_t`. To see how this may make a differece: http://stackoverflow.com/questions/19451101/declaring-64-bit-variables-in-c/19453213#19453213 A pedantic solution would use `((int64_t) INT64_C(PUTVALUE))`. Further, as `0x000000000000000F` is unsigned, should not one use `UINT64_C()`? – chux - Reinstate Monica Sep 10 '14 at 14:34
  • @chux No, as ofc the define will cahnge in diferent versions, otherwise I would just write it in the code instead. and the point is. It IS not unsigned. it hast to have a sign whe the first bit gets set. But about the cast you are right. didn't know about that. – dhein Sep 10 '14 at 14:53