0

Is it possible to define a macro called IPHONE_ONLY for conditional compilation that looks like this:

IPHONE_ONLY -(void)myMethod { //method body } 

or

IPHONE_ONLY( -(void)myMethod { //method body }) 
2
  • 1
    Wouldn't surrounding the function with #ifdef work for you? Commented Feb 4, 2013 at 10:41
  • @Shahbaz It would but I'm working with a header file that has about a hundred method definitions and am looking for something that's easier on the eyes (my eyes at least). Commented Feb 4, 2013 at 10:53

4 Answers 4

3

Even though normally you would surround the function with #ifdef, I tested with gcc and indeed the following also works. I don't know if it is standard:

#ifdef IPHONE # define IPHONE_ONLY(...) __VA_ARGS__ #else # define IPHONE_ONLY(...) #endif IPHONE_ONLY(int func(void) { return 12; }) 

I have never seen anyone code like that though. It is quite common to write such a function like this:

#ifdef IPHONE int func(void) { return 12; } #endif 

Your editor also would be much happier with this, since it understands functions, but not function body used as a macro parameter!

Sign up to request clarification or add additional context in comments.

9 Comments

That might work, but it's ... questionable to introduce a comma like that.
@unwind, indeed, one parameter was enough for the macro. Although I'm not sure what you meant and what you had in mind of what could go wrong.
+1 For pointing out the possibility of passing the body of function as a macro parameter. Although it doesn't seem to be right thing to do.
@LihO, I agree. I just tested it out of curiosity (and to literally answer the question). I would absolutely not recommend it though.
As soon as you have a comma in your function, this will not work. E.g if you have an initialization int x[] = { 1, 2 };. You'd have to go with the solution in my answer to avoid that.
|
2

Sure you can, but I don't think that this would make your code more readable

#if on_iphone # define IPHONE_ONLY(...) __VA_ARGS__ #else # define IPHONE_ONLY(...) #endif 

and then you can use the macro the 2nd way you have it in your question.

But this is really ugly and against the visual expectations of anybody who is used to read proper C.

Comments

1

I think you are looking for ifdef:

#ifdef IPHONE_ONLY void myMethod(){ //method body ) #endif 

Comments

0
#ifdef IPHONE_ONLY # define MY_METHOD() myMethod(); void myMethod() { ...... } #else # define MY_METHOD() #endif 

in your c code of your project, you call MY_METHOD().

This will avoid you to call myMethod() in that way each time you need it in your code

 for(i=0; i<10; i++) { // some thing to do here #ifdef IPHONE_ONLY myMethod(); #endif // other thing to do here } 

with the definition above you will call your myMethod(); in this way

 for(i=0; i<10; i++) { // some thing to do here MY_METHOD(); // other thing to do here } 

If IPHONE_ONLY is defined then the prepocessor will change the macro MY_METHOD() by the call of the function myMethod();

If IPHONE_ONLY is not defined then the preprocessor will change the MY_METHOD() by nothing. It's like the macro call MY_METHOD() does not exist in your code. and the function void myMethod() will not be defined

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.