Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Header Inclusion

Reply
Thread Tools

C++ Header Inclusion

 
 
Matthew Burgess
Guest
Posts: n/a
 
      08-28-2003
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.


 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      08-28-2003
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


 
Reply With Quote
 
 
 
 
Mike Smith
Guest
Posts: n/a
 
      08-28-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in inclusion of header files vector.h,map.h.. kpoan C++ 0 08-15-2006 03:38 AM
tool to check multiple inclusion of header file techBoy C Programming 6 03-15-2006 10:49 AM
Header inclusion question Johannes Bauer C++ 6 12-08-2004 02:53 AM
[C++] Order of header inclusion qazmlp C++ 9 08-25-2003 05:12 AM
Header Inclusion style qazmlp C Programming 14 07-10-2003 11:41 AM



Advertisments