-4

In his book "C++ From The Ground Up" Herbert Schildt wrote that "However, the role of the preprocessor in C++ is much smaller than it is in C. One reason for this is that many of the chores that are performed by the preprocessor in C are performed by language elements in C++. Stroustrup has stated his desire to render the preprocessor redundant, so that,ultimately, it could be removed from the language entirely."

Best examples can be :

  1. use of const keyword instead of #define

  2. use of inline function instead of macro.

Why preprocessor isn't so important in C++?

I just want to know that is preprocessor is really evil? Because Marshall Cline said that preprocessor is evil in this link: http://www.parashift.com/c++-faq/preprocessor-is-evil.html

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Destructor
  • 14,123
  • 11
  • 61
  • 126
  • 12
    ... because of things such as **const**, **inline** and templates. You seem to have answered your own question. Is there anything more specific you are wondering about? – Magnus Hoff Sep 19 '14 at 10:49
  • 2
    However, `__LINE__` and `__FILE__` still stay useful (e.g. for debugging or logging messages). – Basile Starynkevitch Sep 19 '14 at 10:57
  • 1
    Please please don't read bad C++ books and ruin your interest in learning a subject matter altogether. (Bull) Schildt books are notorious for this. – legends2k Sep 19 '14 at 10:57
  • @legends2k: What about the above cited passage is incorrect? – Lightness Races in Orbit Sep 19 '14 at 10:58
  • Note that it's macros which are less necessary in C++ (though they can still do a lot of things that can't be done in any other way), `#include` and `#if`/`#ifdef`/`#endif` etc. are all still highly desirable and have no new alternatives. – Tony Delroy Sep 19 '14 at 10:59
  • @LightnessRacesinOrbit Nothing per-se, but the comment is an advice for the long run - similar to answering just what the question asks vs answering to make the asker think on his own. – legends2k Sep 19 '14 at 11:01
  • @legends2k : Do you think that herbert schildt's books are bad for learning programming languages like c,c++,java etc? I listened that he was member of ANSI/ISO C & ANSI/ISO C++ committee that standardized C & C++ respectively. On which basis you says herbert schildt's book aren't good? – Destructor Sep 20 '14 at 12:56
  • 1
    @meet: The basis is that his books are riddled with numerous errors and bad practices, teaching you poor programming style. Here're some links (search for _Schildt_ in them): [1](http://www.catb.org/jargon/html/B/bullschildt.html), [2](http://www.seebs.net/c/c_tcn4e.html), [3](http://www.lysator.liu.se/c/schildt.html), [4](http://www.faqs.org/faqs/C-faq/learn/), [5](http://c-faq.com/ansi/avail.html), [6](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior/4105123#4105123); oh, and the infamous `void main` was popularized by him. – legends2k Sep 20 '14 at 20:17

1 Answers1

2

The pre-processor was a good idea, at the time it was introduced in the C language (as a text-based generation/replacement of code).

Unfortunately, it introduces a lot of problems that are not readily apparent:

  • they cause the compiler to speak about code that isn't there (at least, it isn't there until after pre-compilation)
  • they break the principle of least surprise, when used to replace functions (consider this macro #define max(a, b) (a) > (b) ? (a) : (b) used like this:

    int x = max(a++, b); // a++ > b ? a++ : b; // _may_ increment a twice,
                                               // depending on a and b
    
  • they are difficult to write correctly, requiring all kinds of work-arounds (like the use of parenthesis around all macro parameters).

Because of all these reasons (and probably others) C++ offers better alternatives (sometimes, designed to replace specific uses of the precompiler).

utnapistim
  • 26,809
  • 3
  • 46
  • 82