macros standard equivalent?

A

aaragon

Hello All,

I am using the following macros;

#define str(x) # x
#define conc(x,y) x ## y

to play with strings. Does anyone know if there is an equivalent
provided by the standard library to accomplish this? Thank you,

aa
 
V

Victor Bazarov

aaragon said:
Hello All,

I am using the following macros;

#define str(x) # x
#define conc(x,y) x ## y

to play with strings. Does anyone know if there is an equivalent
provided by the standard library to accomplish this? Thank you,

No, there isn't anything. However, to properly concatenate those
you might want to use indirection, i.e. have another set of the two
macros to help:

#define applystringize(x) # x
#define str(x) applystringize(x)
#define applyconcatenate(x,y) x ## y
#define conc(x,y) applyconcatenate(x,y)

because otherwise if you use macros like 'conc(x, str(x))', it
won't do it without this extra indirection stuff.

V
 
P

Phlip

aaragon said:
I am using the following macros;

#define str(x) # x
#define conc(x,y) x ## y

to play with strings. Does anyone know if there is an equivalent
provided by the standard library to accomplish this? Thank you,

Those are "stringerization" and "token pasting". They are one of a few
reasons to use macros, and nothing below the macro layer, in the layer of
C++ keywords, can do anything like them. They change the text that the see.
So

conc(in,t) x = 42;

gets compiled as

int x = 42;

Nothing in the standard library can ever do that!

(May we ask what you use those macros for? Situations that actually need
them are kind'a rare...)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top