CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: macro problem

  1. #1
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    macro problem

    Code:
    #define SOMEPRINTF 1 ? (void)0 : printf // 0 to enable and 1 to disable
    this does not pose me a problem ( for 1 and 0) but if i change it this way
    Code:
    #define SOMEPRINTF 1 ? printf : (void)0 // 1 to enable and 0 to disable
    the compiler shows me error : called object is not a function

    i am using gcc 2.95

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: macro problem

    use -save-temps compiler option and check what your macro expands to. I bet it is something nasty.
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: macro problem

    You cannot use 0 as no-op and the macro will be expanded out. In the first case you will get your code expanded to

    Code:
     1 ? (void)0 : printf( args )
    which makes sense because you can pass args to printf but in the latter case it is expanded to

    Code:
     1 ? printf : (void)0( args )
    which won't compile.

    Better would be to declare a no-op printf thus:
    Code:
    int no_op( ... )
    {
     // does nothing
    }
    Now get your macro to return a function to a pointer and call it on the args, so you'll need brackets thus:
    Code:
    #define SOMEPRINTF (1 ? printf : no_op )
    and that might work.

  4. #4
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: macro problem

    If I diable it or enable there is no problem with the first declaration.
    Code:
    #define SOMEPRINTF 1 ? (void)0 : printf // 0 to enable and 1 to disable
    This compiles fine. Why is it so? Does it not expand to the same nasty expansion as
    Code:
    (void)0( args )

  5. #5
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: macro problem

    Quote Originally Posted by NMTop40
    Better would be to declare a no-op printf thus:
    Code:
    int no_op( ... )
    {
     // does nothing
    }
    Now get your macro to return a function to a pointer and call it on the args, so you'll need brackets thus:
    Code:
    #define SOMEPRINTF (1 ? printf : no_op )
    and that might work.
    This will have an extra call. I need to complelety disable the printf. Well, actually those are debug calls which I would like to disable when compiling for release. Any other alternate solutions?

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: macro problem

    You can try the following example.

    Code:
    #ifdef TURNON_DEBUG
    
    #define SOMEPRINTF printf
    
    #else
    
    #define SOMEPRINTF
    
    #endif
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  7. #7
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: macro problem

    Quote Originally Posted by Kheun
    You can try the following example.

    Code:
    #ifdef TURNON_DEBUG
    
    #define SOMEPRINTF printf
    
    #else
    
    #define SOMEPRINTF
    
    #endif
    That would leave the arguments floating around in space. I think that's allowed, but you could define SOMEPRINTF to be no_op as I defined it.

  8. #8
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: macro problem

    Quote Originally Posted by kumaresh_ana
    This will have an extra call. I need to complelety disable the printf. Well, actually those are debug calls which I would like to disable when compiling for release. Any other alternate solutions?
    The compiler would see no_op is empty and strip it out. If this is C++ or C99 you can use the inline keyword to advise the compiler to do just that.

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: macro problem

    Quote Originally Posted by NMTop40
    That would leave the arguments floating around in space. I think that's allowed, but you could define SOMEPRINTF to be no_op as I defined it.
    Yes, it is allowed. In fact, the floating arguments are being optimized out from the compiled code.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  10. #10
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: macro problem

    No. My compiler throws error upon seeing floating arguements.

  11. #11
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: macro problem

    Would you give me a sample of the code and what compiler are you using? It is working on VC and GCC (if I recalled correctly).
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  12. #12
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: macro problem

    I am using gcc 2.95 but not intel platform. Its an embedded processor.

    Here are some samples.....
    Code:
    SOMEPRINTF("kum debug-> xx calling yy");
    
    SOMEPRINTF("kum debug-> xx val is %d", xx);

  13. #13
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: macro problem

    My linux machine is installed with GCC 3.2.3 and your sample compiles fine without any error. The only way I can see the warning messages only if I set to higher warning level. e.g. gcc test.c -W -o test.out.

    test.c: In function `main':
    test.c:19: warning: left-hand operand of comma expression has no effect
    test.c:18: warning: statement with no effect
    test.c:19: warning: statement with no effect
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  14. #14
    Join Date
    Jun 2006
    Location
    Bangalore, India
    Posts
    332

    Re: macro problem

    I think I need to change the compiler. Thanks for your help guys.

  15. #15
    Join Date
    Aug 2006
    Posts
    38

    Arrow Re: macro problem

    hey kumar



    you can go for devc++

    http://www.bloodshed.net/devcpp.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured