Header file multiple inclusion question

This is a discussion on Header file multiple inclusion question within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by cpjust So in the case where you'd actually want to include multiple times, they could have added ...

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,452
    Quote Originally Posted by cpjust View Post
    So in the case where you'd actually want to include multiple times, they could have added a #pragma many statement (the opposite of #pragma once).
    I disagree. The bottom line is that the programmer has to understand what will happen when any given input file is fed to the compiler. Introducing #pragma's (which are a standard means of allowing compiler specific extensions) to work around lack of programmer understanding is not a particularly good approach.

    The basic issue here is that the preprocessor works by doing text substitution, whereas the C++ (and C) language are token based. The advocates of things like #pragma once tend to incorrectly assume the preprocessing phase honours rules of the language accepted by the compilation phase (hence the fact that such things affect lexical analysis by the compiler).

    Quote Originally Posted by cpjust View Post
    That way in 99.999% of header files where you don't want it to be included more than once, you don't have to worry about adding include guards (which is a common noob mistake), and only in the rare cases when you do want it multiply included would you need to add an extra line of code.
    But we're stuck with the way it works now, so it doesn't make much difference what I think might have been a good feature...
    The real solution is either to properly understand the preprocessor (include guards are something quite consistent with how the preprocessor works) or for the language to include some scheme other than the preprocessor for separating declarations from definitions (which happens with a number of languages other than C or C++). Both approaches have their trade-offs, despite each having strong advocates.

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by grumpy View Post
    I disagree. The bottom line is that the programmer has to understand what will happen when any given input file is fed to the compiler. Introducing #pragma's (which are a standard means of allowing compiler specific extensions) to work around lack of programmer understanding is not a particularly good approach.

    The basic issue here is that the preprocessor works by doing text substitution, whereas the C++ (and C) language are token based. The advocates of things like #pragma once tend to incorrectly assume the preprocessing phase honours rules of the language accepted by the compilation phase (hence the fact that such things affect lexical analysis by the compiler).


    The real solution is either to properly understand the preprocessor (include guards are something quite consistent with how the preprocessor works) or for the language to include some scheme other than the preprocessor for separating declarations from definitions (which happens with a number of languages other than C or C++). Both approaches have their trade-offs, despite each having strong advocates.
    The whole point of compilers is to make programming easier, otherwise we'd all be coding in binary machine code. I've never had a problem with using header guards, but it is more typing, and you need to come up with unique names for the guards...
    If the compiler & pre-processor worked the way I described, then understanding how the pre-processor worked would mean understanding that you don't need header guards since it would take care of that automatically and only in the exceptional cases would you need to do something extra.
    The goal is to make life easier, not more complicated.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  3. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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