Macro that Defines another on invocation

A

aravindh.k

Hi,

I want to define a macro that defines another on invocation. Somthing
like this:

#define MACRO_NR_1(FLAG, VALUE) #define MACRO_NR_1_##FLAG(VALUE)
(VALUE+FLAG)

So that when I call MACRO_NR_1(10), the macro MACRO_NR_1_10 gets
defined. The macro above does not work.

Do you know how I may do this ?

Sincerely,
Aravindh
 
P

Pieter Droogendijk

Hi,

I want to define a macro that defines another on invocation. Somthing
like this:

#define MACRO_NR_1(FLAG, VALUE) #define MACRO_NR_1_##FLAG(VALUE)
(VALUE+FLAG)

# is, in fact, a pre-processor operator. It's operand should be one of the
function-like macro's parameters, the 'value' of which will then be
magically transformed into a string literal.

So this is definitely not going to work.
So that when I call MACRO_NR_1(10), the macro MACRO_NR_1_10 gets
defined. The macro above does not work.

Do you know how I may do this ?

You may not.

6.10.3.4 #3: "The resulting completely macro-replaced preprocessing token
sequence is not processed as a preprocessing directive even if it
resembles one."

The "resulting completely macro-replaced preprocessing token sequence"
being your fully processed macro replacement.
 
A

aravindh.k

dank u.

I will not try it again. I plan to use inline functions now.
Thanks for pasting the standard, now I know it cannot be done in ANSI C.
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top