C macro to expand struct types?

D

Doug .

Howdy,

I'm using the following hackery to define namespaces for structs:

#define GENERIC_NS_PREFIX default_
#define GENERIC_CONCAT(p1, p2) GENERIC__CONCAT(p1, p2)
#define GENERIC__CONCAT(p1, p2) p1 ## p2
#define $(name) GENERIC_CONCAT(GENERIC_NS_PREFIX, name)

#undef GENERIC_NS_PREFIX
#define GENERIC_NS_PREFIX namespace1_
struct $(List) *list;

#undef GENERIC_NS_PREFIX
#define GENERIC_NS_PREFIX namespace2_
struct $(List) *list;

This works perfectly, and yields:

arc:~ douglasl$ cc -E junk.h
# 1 "junk.h"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "junk.h"

struct namespace1_List *list;
struct namespace2_List *list;

So, my question is this:

Why do I need three macros to do, essentially:

#define X x
#define Y(y) X ## __ ## y

Surely there is some syntax I can use that doesn't require 3 nested macros to achieve this?

cheers,
Doug.
 
S

Shao Miller

...
So, my question is this:

Why do I need three macros to do, essentially:

#define X x
#define Y(y) X ## __ ## y

Surely there is some syntax I can use that doesn't require 3 nested
macros to achieve this?

I can't think of any. Sorry.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top