Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8016983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:42:00+00:00 2026-06-04T20:42:00+00:00

Is there any way to do the following at compile-time? int anInteger = 0;

  • 0

Is there any way to do the following at compile-time?

int anInteger = 0;
__if_object(anInteger) {
    // send object some messages
}
__if_primitive(anInteger) {
    // do something else
}

An dummy situation where this could be used is to define the __add_macro below.

#define __add_macro(var, val) __something_goes_here__

int i = 1;
MyInteger* num = [[MyNumber alloc] initWithValue:1]

__add_macro(i, 4);
__add_macro(num, 4);

// both should now hold 5

Clarification/Simplification

I guess there is no way to do this with one macro. But I still need it to warn if the macro is being used on the wrong datatype. Those two types are: object and non-object).

To check if it is an object, this works:

#define __warn_if_not_object(var) if(0){[(var) class];}

What I need:

#define _warn_if_object(var) if(0){__something_here__}

Again, I need this to happen at compile-time. And it can either throw an error or warning.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:42:02+00:00Added an answer on June 4, 2026 at 8:42 pm

    When you declare an int variable you can really only put an int value in it.

    While this is Objective-C, and hence C, so you can bypass just about every type protection mechanism that exists, this is not to be advised. Indeed there is no guarantee whatsoever that a, say, NSNumber reference will even fit into an int variable – and more than enough chance that if you try, and bypass any warnings, some bits will just get tossed making the reference invalid.

    So, no, while you can tell what class an object reference refers to, you cannot in general tell whether a variable has an integer value or an object reference in it – you shouldn’t even try to put these two very different things into the same variable.

    Answer 2

    Patrick, your comments and clarification seem to suggest you are not trying to do what the question starts out by asking (how do you determine if the value in an int is an object – answered above, you don’t), but something rather different…

    I think what you’re after is function overloading, and as you seem to be trying to use macros, maybe inline functions as well. Clang supports function overloading, here is program fragment which may show you how to solve your problem:

    // Clang likes prototypes so let's give it some
    // The following declares two overloaded inline functions:
    NS_INLINE void __attribute__((overloadable)) byType(int x);
    NS_INLINE void __attribute__((overloadable)) byType(NSNumber *x);
    
    // now some simple definitions:
    NS_INLINE void __attribute__((overloadable)) byType(int x)
    {
       NSLog(@"int version called: %d", x);
    }
    
    NS_INLINE void __attribute__((overloadable)) byType(NSNumber *x)
    {
       NSLog(@"NSNumber version called: %@", x);
    }
    
    // now call them, automatically selecting the right function
    // based on the argument type
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
       int x = 5;
       NSNumber *y = [NSNumber numberWithInt:42];
    
       byType(x);
       byType(y);
    }
    

    The above code when run outputs:

    int version called: 5
    NSNumber version called: 42
    

    Clang 3 compiles the above code inlining the two calls, so you get the same code as using macros.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering is there any way to execute following shell script, which waits
Is there any way to combine the following and set @FH_RecordKey and @VF_dept_seq in
Is there any way to do the following: validateLogin(); return false; But actually like
Is there any way I could run the following 'logical code' to actually work?
Is there any way to create a constant object(ie it cannot be edited and
Is there any way in Notepad++ (or even with another tool) to change the
Is there any way I can set a formatter on models that will convert
Is there any way to use Google's API to retrieve a user's current zipcode
Is there any way to overload the > (greater than) operator, to be able
Is there any way we can fetch X509 Public Cetrificates using c# from AD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.