Thread: Question about macros

  1. #1
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53

    Question about macros

    Hi, I have a question about macros. I am developing a cpp program that will be run as a standalone exe. The exe will run with a parameter inverse=true or false.

    Code:
    test.exe -inverse=true OR test.exe -inverse=false.
    I wish to define 2 macros prefix and suffix such as their values should depend on the command line parameter inverse.

    When inverse is true,

    Code:
    #define PREFIX "P5F"
    #define SUFFIX "P5J"
    Whereas, when inverse is false,

    Code:
    #define PREFIX "P5J"
    #define SUFFIX "P5F"
    How can I achieve this? Is there a way I can define a macro so that the value of PREFIX and SUFFIX are set depending on the value of parameter "inverse".

    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Nope. Macros are fixed at compile time. They can't be made to depend on a runtime value.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Apprentice
    Join Date
    Oct 2006
    Location
    LA
    Posts
    53
    Thanks for your reply brewbuck. So a better appraoch would be to use a const char pointer in my code instead of a macro?

    Something like

    Code:
    const char* prefix = NULL;
    const char* suffix=NULL;
    
    if(inverse==true)
    {
        prefix = "P5F";
        suffix = "P5J";
    }
    else
    {
        prefix = "P5J";
        suffix = "P5F";
    }
    Is this a good way?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Indeed.

    I personally wouldn't do "if (inverse == true)", but do "if (inverse)".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Indeed.

    I personally wouldn't do "if (inverse == true)", but do "if (inverse)".

    --
    Mats
    I always do if ( something == true ) or if ( ptr != NULL ) because it's more explicit and easier to read if you're skimming through the code quickly; and it was one of the coding standards at my first company.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Doesn't it just increase the risk of

    Code:
    if (something = true)
    ?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by anon View Post
    Doesn't it just increase the risk of

    Code:
    if (something = true)
    ?
    I've seen people write it backwards to prevent this error.

    if(constant = something)

    This will always yield an error.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Desolation
    This will always yield an error.
    Yes, assuming that the left hand expression is an rvalue or const. However, because this does not map to the usual way one would say it, I would argue that this would then make it harder "to read if you're skimming through the code quickly".

    Basically, I see no advantage to comparing to a boolean literal. If the variable is not a bool, and you want to be explicit about it, then compare with an appropriate value (e.g., zero), or call an appropriate named function (e.g., use the fail() member function of an input stream instead of relying on an implicit conversion). In that sense, I see some value with comparing a pointer with a null pointer constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    Doesn't it just increase the risk of

    Code:
    if (something = true)
    ?
    Yes, but any decent compiler should give you a warning when you do this.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    I always do [B]if ( something == true )
    Well, then shouldn't you be doing:

    Code:
    if( ( something == true ) == true )
    And if you're going to do that, might as well:

    Code:
    if( ( ( something == true ) == true ) == true )
    etc...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    Well, then shouldn't you be doing:

    Code:
    if( ( something == true ) == true )
    And if you're going to do that, might as well:

    Code:
    if( ( ( something == true ) == true ) == true )
    etc...
    That would make no sense. That's not being any more explicit, that's just being crazy.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But it is more explicit! It is! It is!

    What about:

    Code:
    if ((something == false) == true)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    That would make no sense. That's not being any more explicit, that's just being crazy.
    Just to clarify: under what situations did your first company's coding standard mandate the comparison with a boolean literal?

    As I noted in post #8, I can understand the comparison with a null pointer constant to be explicit, and of course the use of a named function instead of an implicit conversion (directly or indirectly) to bool. But to compare a boolean flag or a boolean value returned by a function call with a boolean literal?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    That would make no sense. That's not being any more explicit, that's just being crazy.
    Just to clarify: under what situations did your first company's coding standard mandate the comparison with a boolean literal?

    As I noted in post #8, I can understand the comparison with a null pointer constant to be explicit, and of course the use of a named function instead of an implicit conversion (directly or indirectly) to bool. But to compare a boolean flag or a boolean value returned by a function call with a boolean literal?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by laserlight View Post
    Yes, assuming that the left hand expression is an rvalue or const. However, because this does not map to the usual way one would say it, I would argue that this would then make it harder "to read if you're skimming through the code quickly".

    Basically, I see no advantage to comparing to a boolean literal. If the variable is not a bool, and you want to be explicit about it, then compare with an appropriate value (e.g., zero), or call an appropriate named function (e.g., use the fail() member function of an input stream instead of relying on an implicit conversion). In that sense, I see some value with comparing a pointer with a null pointer constant.
    I did not say I used this form. It feels backwards to me and I wouldn't use it. Just felt like mentioning the alternative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Quick question about VB Word macros
    By PJYelton in forum Tech Board
    Replies: 6
    Last Post: 05-28-2005, 10:26 PM
  3. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Macros question
    By anson1999 in forum C Programming
    Replies: 12
    Last Post: 03-08-2003, 10:30 PM