[Objective] C Preprocessor macro possible

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
I have a bunch of code that relies on a preprocessor constant.

I'd like to have a macro that conceptually works like this:

#define A_OR_B(a,b) \
#ifdef A \
(a) \
#else \
(b) \
#endif

And I'd use it like abc(A_OR_B(1, 2));

Right now I'm doing:
#ifdef A
abc(1)
#else
abc(2)
#endif

And I don't like that. Any suggestions? I'm using objective C. I thought I might be able to do something with a C++ template but objective C isn't a superset of C++, only C.

I could have a function that that does the check but I'd rather not have the function call overhead... but right now I think that is a better idea than all my #ifdefs...
 

mosco

Senior member
Sep 24, 2002
940
1
76
What's your concern about function call overhead? Is this going to be called a lot?

Can you give some more detail about what functionality you trying to switch between?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,838
4,817
75
Well, this works in C:
Code:
#ifdef A
#define A_OR_B(a,b) (a)
#else
#define A_OR_B(a,b) (b)
#endif
 

uclabachelor

Senior member
Nov 9, 2009
448
0
71
I have a bunch of code that relies on a preprocessor constant.

I'd like to have a macro that conceptually works like this:

#define A_OR_B(a,b) \
#ifdef A \
(a) \
#else \
(b) \
#endif

And I'd use it like abc(A_OR_B(1, 2));

Right now I'm doing:
#ifdef A
abc(1)
#else
abc(2)
#endif

And I don't like that. Any suggestions? I'm using objective C. I thought I might be able to do something with a C++ template but objective C isn't a superset of C++, only C.

I could have a function that that does the check but I'd rather not have the function call overhead... but right now I think that is a better idea than all my #ifdefs...


I'd go with Keng6 wrote or, something like:

Code:
#ifdef A
#define A_OR_B a
#else
#define A_OR_B b
#endif

Your original code is too hard to read. Keep it clean!
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
What's your concern about function call overhead? Is this going to be called a lot?

Can you give some more detail about what functionality you trying to switch between?

Not really concerned with the overhead as much as I think there should be a better way.

I have 2 targets in my project that have different features enabled and I have some coordinates that depend on the target.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Well, this works in C:
Code:
#ifdef A
#define A_OR_B(a,b) (a)
#else
#define A_OR_B(a,b) (b)
#endif

I don't think that works. It would require the pre processor to make 2 passes which I don't think it does. I think that was the first thing I tried but maybe I'm wrong.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,838
4,817
75
I don't think that works. It would require the pre processor to make 2 passes which I don't think it does. I think that was the first thing I tried but maybe I'm wrong.

It works. I tested it. :colbert:

Code:
#include <stdio.h>
//#define A
#ifdef A
#define A_OR_B(a,b) (a)
#else
#define A_OR_B(a,b) (b)
#endif

int main(void) {
  printf("A OR B is %s\n", A_OR_B("A", "B"));

  return 0;
}
Uncomment #define A to change the output of A_OR_B.