C++ Header Inclusion

Discussion in 'C++' started by Matthew Burgess, Aug 28, 2003.

  1. According to 17.4.4.1 paragraph 1 in the standard "A C++ header may include
    other C++ headers." This gives rise to what I consider to be a portability
    problem, albeit trivially corrected. Consider this code that compiles
    correctly on Sun's Forte 7 compiler:

    #include <string>
    int main() {
    std::string text = "34";
    std::atoi(text.c_str());
    return 0;
    }

    Obviously, std::atoi comes from the <cstdlib> header which, due to the lack
    of compilation errors one can assume is #included within <string>. I have a
    feeling that other implementations may not see a requirement for <cstdlib>
    features in their implementation of std::string and therefore the code will
    require fixing if it is to be ported to such an implementation.

    My question therefore is: Is there a need for the standard to include some
    kind of safeguards to aid in portability, e.g. state that a compiler warning
    be generated in the event that such a scenario as described above may occur?

    Regards,

    Matt.
     
    Matthew Burgess, Aug 28, 2003
    #1
    1. Advertising

  2. Matthew Burgess

    Attila Feher Guest

    Matthew Burgess wrote:
    > According to 17.4.4.1 paragraph 1 in the standard "A C++ header may
    > include other C++ headers." This gives rise to what I consider to be
    > a portability problem, albeit trivially corrected. Consider this
    > code that compiles correctly on Sun's Forte 7 compiler:
    >
    > #include <string>
    > int main() {
    > std::string text = "34";
    > std::atoi(text.c_str());
    > return 0;
    > }
    >
    > Obviously, std::atoi comes from the <cstdlib> header which, due to
    > the lack of compilation errors one can assume is #included within
    > <string>. I have a feeling that other implementations may not see a
    > requirement for <cstdlib> features in their implementation of
    > std::string and therefore the code will require fixing if it is to be
    > ported to such an implementation.
    >
    > My question therefore is: Is there a need for the standard to include
    > some kind of safeguards to aid in portability, e.g. state that a
    > compiler warning be generated in the event that such a scenario as
    > described above may occur?


    This is not really possible to solve with the file/inclusion model. To
    overcome this one either needs to use a lint checker. And you do not really
    want to lint your code each time you compile it. So puting it into the
    compiler by the standard would be an overkill.

    A "module" based approach could solve such issues, but then again (IMHO)
    that would also be too big change for the language. :-(

    --
    Attila aka WW
     
    Attila Feher, Aug 28, 2003
    #2
    1. Advertising

  3. Matthew Burgess

    Mike Smith Guest

    Matthew Burgess wrote:

    > According to 17.4.4.1 paragraph 1 in the standard "A C++ header may include
    > other C++ headers." This gives rise to what I consider to be a portability
    > problem, albeit trivially corrected. Consider this code that compiles
    > correctly on Sun's Forte 7 compiler:
    >
    > #include <string>
    > int main() {
    > std::string text = "34";
    > std::atoi(text.c_str());
    > return 0;
    > }
    >
    > Obviously, std::atoi comes from the <cstdlib> header which, due to the lack
    > of compilation errors one can assume is #included within <string>. I have a
    > feeling that other implementations may not see a requirement for <cstdlib>
    > features in their implementation of std::string and therefore the code will
    > require fixing if it is to be ported to such an implementation.


    Well, the better rule to follow is that if *you* use <cstdlib>, you
    should include it, regardless of whether it is used by <string>.

    --
    Mike Smith
     
    Mike Smith, Aug 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    9
    Views:
    580
    Andre Kostur
    Aug 25, 2003
  2. Johannes Bauer

    Header inclusion question

    Johannes Bauer, Dec 7, 2004, in forum: C++
    Replies:
    6
    Views:
    452
    EventHelix.com
    Dec 8, 2004
  3. qazmlp

    Header Inclusion style

    qazmlp, Jul 8, 2003, in forum: C Programming
    Replies:
    14
    Views:
    574
    Casper H.S. Dik
    Jul 10, 2003
  4. kpoan
    Replies:
    0
    Views:
    489
    kpoan
    Aug 15, 2006
  5. techBoy
    Replies:
    6
    Views:
    354
    Vladimir S. Oka
    Mar 15, 2006
Loading...

Share This Page