• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

[Objective] C Preprocessor macro possible

nickbits

Diamond Member
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...
 
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?
 
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 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!
 
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.
 
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.
 
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.
 
Back
Top