[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

C macro to expand struct types?

linderd

7/1/2011 1:32:00 AM

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.
1 Answer

Shao Miller

7/3/2011 7:30:00 PM

0

On Thu, 30 Jun 2011 18:31:34 -0700 (PDT), "Doug ."
<douglas.linder@gmail.com> wrote:
> ...
> 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.