single-quoting a macro argument

M

Marius Lazer

So you're using the C preprocessor to produce code in a language other
than C?

Have you considered using some other preprocessor? (m4, maybe?)


If the fact that it happens to work on one specific implementation is
good enough for you, that's fine. You should just be aware that it
invokes undefined behavior, and it could easily fail in arbitrarily
bad and subtle ways under other implementations.

I'm not playing tricks. I'm using embedded SQL in C/C++ code (Pro*C/C++
for Oracle and Esql-C/CC for Ingres) and no, I don't like "hacky"
solutions. Fortunatelly, the trick proposed by Robert and the one below
won't compile if the *feature* is not supported by the compiler's
preprocessor.

I did some reading on Boost preprocesoor metaprogramming and here's
another solution (perhaps I rushed bugging you folks):

#include <boost/preprocessor/cat.hpp>

#define QUOTE(x) '
#define START_QUOTE(x) BOOST_PP_CAT(QUOTE(x), x)
#define QUOTE_ARG(x) BOOST_PP_CAT(START_QUOTE(x), QUOTE(x))

QUOTE_ARG(foo) // expands to 'foo' on gcc 4.0.2

I would have liked this to work but I get a cpp error even though it
still expands to 'foo':

#define QUOTE(x) '
#define QUOTE_ARG(x) BOOST_PP_CAT(QUOTE(x), BOOST_PP_CAT(x, QUOTE(x)))

Marius
 
P

peyote

Marius Lazer ha scritto: > > > > So you're using the C preprocessor
to produce code in a language other > > than C? > > > > Have you
considered using some other preprocessor? (m4, maybe?) > > > > >
Anyway, this does it (I tried it on gcc 4.0.2). > > > > If the fact
that it happens to work on one specific implementation is > > good
enough for you, that's fine. You should just be aware that it > >
invokes undefined behavior, and it could easily fail in arbitrarily > >
bad and subtle ways under other implementations. > > > > I'm not
playing tricks. I'm using embedded SQL in C/C++ code (Pro*C/C++ > for
Oracle and Esql-C/CC for Ingres) and no, I don't like "hacky" >
solutions. Fortunatelly, the trick proposed by Robert and the one below
 

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

Similar Threads


Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top