Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macro dependent macro

Is it possible to do something like this:

  #define F(x) \
    #ifdef DOUBLE \
      2*x \
    #else \
      x \
    #endif

so that when I use F, what it expands to depends on whether the macro DOUBLE is defined? I don't think so, but I'm hopeful. GNU expansions are fine.

Edit In response to some of the answers, I'm really using this to do some code generation, where the code is slightly different depending on where it gets defined. Because of the order in which some files are included and where the relevant macros need to be defined, switching it around that way requires a bit of factoring. I may have to do it, but would be thrilled if I don't have to unpaint myself from this corner!

like image 786
pythonic metaphor Avatar asked Feb 07 '11 23:02

pythonic metaphor


1 Answers

What's wrong with

#ifdef DOUBLE
  #define F(x) (2 * (x))
#else
  #define F(x) (x)
#endif
like image 102
Daniel Gallagher Avatar answered Oct 03 '22 00:10

Daniel Gallagher