Const and macro

This is a discussion on Const and macro within the C++ Programming forums, part of the General Programming Boards category; Hi, can any one give proper reason why should i use const as compaire to 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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    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
    20,975
    Yes. Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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,317
    >> 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,317
    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,317
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Page 1 of 3 123 LastLast
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, 10: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, 12:26 AM
  5. c and c++
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 04:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21