Thread: Const and macro

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    India
    Posts
    17

    Const and macro

    Hi,
    can any one give proper reason why should i use const as compaire to macro ?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brw_Abhi View Post
    Hi,
    can any one give proper reason why should i use const as compaire to macro ?
    An evil header file can redefine your macro without your knowledge. A header file trying to redefine your const will throw a compile error.

    You can take the address of a const -- can't do that with a macro.

    Why use a hack (macros) when there are finally better methods available?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
    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

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    the only legitimate use of #macro preprocessor seems to be for skipping unneeded parts at compile time.
    for example, use #ifdef etc, but, that's all
    there are much better ways in C++ than to do this silly thing:
    #define PI 3.14if0rg0t

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, macros have so much more use than that, but they are also evil at the same time, so avoiding them and using them wisely is a good idea.
    (They can, for example, increase readability by picking away large chunks of code that can't be shortened by typedefs [for example, templates], among other things.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Elysia View Post
    No, macros have so much more use than that, but they are also evil at the same time, so avoiding them and using them wisely is a good idea.
    (They can, for example, increase readability by picking away large chunks of code that can't be shortened by typedefs [for example, templates], among other things.)
    Tools are not innately evil, people can only use the in evil ways.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, macros can't be separated by namespaces, so macro name collisions are much more common than variables separated by namespaces.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Tools are not innately evil, people can only use the in evil ways.
    Calling a tool "evil" that makes it unecessarily easy for users to use it in evil ways is acceptable.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Pointers and references are evil, too, then I guess. C++ is an evil language. Look at the majority of questions on the forum. People doing careless things easily.

    Blame the carpenter, not the tool.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do pointers and references make it unnecessarily easy for a user to use them evilly? Are there alternatives that are harder to do evil with?

    Blame the tool when the tool deserves blame. If you buy a nail gun that shoots the nails in both directions, then that nail gun is evil. Yes, you should be careful and read the directions and only use that nail gun when you have to drive a nail into two different directions, but the tool still certainly deserves blame for all the people with nails in their eye.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Daved View Post
    Are there alternatives that are harder to do evil with?
    [strawman argument elided]

    Sure, there are alternatives - use Java or C# instead. The overwhelming majority of people that use C++ aren't disciplined or responsible enough to use it and would be better served with a simpler language. I'm tired of seeing the language criticized because of the actions of lazy and/or bad programmers.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It doesn't sound like you understood what I was trying to get at. Then again, you accuse me of using a strawman argument, so perhaps I'm not understanding you. I think you implied that a tool cannot be considered evil. I am claiming that a tool can be considered evil.

    References are not considered evil because they do not unnecessarily allow users to do bad things with them, and there are no alternatives that are less evil.

    Macros are considered evil because they do allow users to do evil (by polluting namespaces, ignoring type rules, etc) and there are generally better alternatives (const, inline, etc).

    So, for someone to call a tool (in this case a feature of C++) evil is completely acceptable because there is an inherent difference between the evil tool and the not evil alternative.
    Last edited by Daved; 05-16-2008 at 11:57 AM.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well I think Java is evil.
    But then again, there are a few parts of C++ I would have liked to have seen done differently, but some of them would have broken compatability with C... Why that's such a bad thing, I'm not sure; just name your C files .c and C++ files .cpp and they should use the right rules for compiling each (but then you'd need to use .hpp files instead of .h for C++).

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Another reason is that you can't change the value of a macro at run time but you can change the value of a const at run time (if you do it right and set things up properly)

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. We might be stretching the rules a bit there if we want to modify the value of a const...
    But it's certainly doable and acceptable in some cases.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Macro has same name as Function?
    By coolclu3 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 04:55 AM
  3. Defining const in a class
    By g4j31a5 in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2006, 11:27 AM
  4. Is an inline function really the same as a macro?
    By Sloede in forum C Programming
    Replies: 6
    Last Post: 01-23-2004, 01:26 AM
  5. c and c++
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 04:50 PM