expending macro between double quotes

P

puzzlecracker

In this silly example say I want NUM macro to expend in printf?

#include<cstdio>
using namespace std;

#define NUM 10
int main(){

printf(" NUM \n");
return 0;
}

I don't need solutions of this type printf(" %d \n", NUM); since in
the production code I don't have this luxury. For now, I use make_str
on top of the call.

Thanks
 
C

Chris Thomasson

puzzlecracker said:
In this silly example say I want NUM macro to expend in printf? [...]

I don't need solutions of this type printf(" %d \n", NUM); since in
the production code I don't have this luxury. For now, I use make_str
on top of the call.

Does this do what you want:
_______________________________________________________
#include <cstdio>

#define QUOTE_IMPL(mp_token)#mp_token
#define QUOTE(mp_token)QUOTE_IMPL(mp_token)

#define NUM 10

int main() {
std::printf(QUOTE(NUM) "\n");
return 0;
}

_______________________________________________________


?
 
P

puzzlecracker

In this silly example say I want NUM macro to expend in printf? [...]

I don't need solutions of this type printf(" %d \n", NUM); since in
the production code I don't have this luxury. For now, I use make_str
on top of the call.

Does this do what you want:
_______________________________________________________
#include <cstdio>

#define QUOTE_IMPL(mp_token)#mp_token
#define QUOTE(mp_token)QUOTE_IMPL(mp_token)

#define NUM 10

int main() {
  std::printf(QUOTE(NUM) "\n");
  return 0;

}

_______________________________________________________

?

It does, but macros are EVIL, and I don't want to introduce another
macro level. I thought the language had some sort of support for that,
perhaps an escape. In my situation, it's no big deal since workaround
is pretty sound.
 
B

Bo Persson

puzzlecracker said:
In this silly example say I want NUM macro to expend in printf?
[...]
I don't need solutions of this type printf(" %d \n", NUM); since
in the production code I don't have this luxury. For now, I use
make_str on top of the call.

Does this do what you want:
_______________________________________________________
#include <cstdio>

#define QUOTE_IMPL(mp_token)#mp_token
#define QUOTE(mp_token)QUOTE_IMPL(mp_token)

#define NUM 10

int main() {
std::printf(QUOTE(NUM) "\n");
return 0;

}

_______________________________________________________

?

It does, but macros are EVIL, and I don't want to introduce another
macro level. I thought the language had some sort of support for
that, perhaps an escape. In my situation, it's no big deal since
workaround is pretty sound.

You started using a macro in the first place. :)

The C++ way is to use a named constant:

#include <iostream>

const int Num = 10;

int main()
{
std::cout << Num << "\n";

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top