preprocessor: how to operate on strings

A

abir

Hi,
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?

i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.

like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -> myID (string, using preprocessor) -> MYID (using
template) .

I am interested in a compile time solution, runtime one is trivial.

thanks
abir basak
 
R

red floyd

abir said:
Hi,
This is not strictly a C++ language question.
Is there any way to form a string from an identifier with the
preprocessor with some operations ?

i.e to say to make a string from an identifier ID i use #ID.
Thats ok, but what if i want to alter it a little.

like if i have an identifier myID , and i want to generate an string
MYID (or any other form) from it.
I am also interested to know if it can be done using template also ,
i.e myID -> myID (string, using preprocessor) -> MYID (using
template) .

I am interested in a compile time solution, runtime one is trivial.

read up on the ## token pasting operator.

#define MY(id) my ## id

MY(Word) // generates myWord
MY(Goodness) // generates myGoodness
 
A

abir

read up on the ## token pasting operator.

#define MY(id) my ## id

MY(Word) // generates myWord
MY(Goodness) // generates myGoodness

Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2
};
i want to make a few strings from it as TYPE1, TYPE2 , where the input
is meType1, meType2 and NOT anything else (which will be done using
some seq preprocessor constructs from boost) .
So to say, i want to apply some construct statically of meType1 to
generate TYPE1 either using preprocessor or using template.

thanks
abir
 
J

James Kanze

Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2};
i want to make a few strings from it as TYPE1, TYPE2 , where
the input is meType1, meType2 and NOT anything else (which
will be done using some seq preprocessor constructs from
boost) .

You can't, really. You have two choices: either write a small
parser yourself (e.g. ignoring everything but enum's), and use
it to generate the strings, or write a small program which
generates both the enum and the strings from some simply
formatted input. I've done the first, and it's not that hard.
And in your case, the second would only be about 10 lines of GNU
awk. (Standard awk doesn't have a toupper function, so you'd
have to implement that as well.)
 
B

Ben Bacarisse

abir said:
Thats true, but i have an identifier myID, and not id. So i can't do
that.
to clarify the point, say i have an enum
enum MyEnum{
meType1,meType2
};
i want to make a few strings from it as TYPE1, TYPE2 , where the input
is meType1, meType2 and NOT anything else (which will be done using
some seq preprocessor constructs from boost) .
So to say, i want to apply some construct statically of meType1 to
generate TYPE1 either using preprocessor or using template.

You might consider using an extra pre-processor step. M4 (a
general-purpose macro processor) can be told to use sufficiently
obscure syntax that it is unlikely to mess with any of your C++ but
will pick out exactly those bits where you want to do this sort of
transformation.
 
B

Ben Bacarisse

Robbie Hatley said:
"abir" asked:


It's on-topic, though. The preprocessor is a part of C++.


Some really nifty things can be done with the preprocessor.
I used the following to cut several hundred messy lines of
error-prone cut-n-paste code down to about a dozen lines in
the program I'm maintaining here at work:

#define AUXI_ITEM(r,t,c) IG ## r ## _ ## t ## c

The "##" directive is a "string paster".

Small point: it is usually called a token paster. # is the one that
turns a macro argument into a string literal.
This macro builds a string out of 3 input strings (t, r, c).

Really, it builds a token out 3 other tokens. I know you know this.
I just think it help to keep the terminology clear.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top