5

I have a simple program in which I arrange the elements of an int array in ascending or descending order, and I use the swap() function to move the elements around. I compiled the program without any errors, and it ran like a charm. I only noticed afterwards that I had forgotten to #include the library that swap() is defined in (<algorithm>, or <utility> as of C++11) before I compiled.

Why did it still work? The top of my program looked like this:

#include <iostream>
#include <cstdlib>
using namespace std;

I tried taking out <iostream>, just to see what would happen, and it predictably put out a bunch of 'cout/cin/endl' was not declared in this scope errors, but I was surprised to see that it gave some 'swap' was not declared in this scope errors as well. Does that mean swap() is defined in <iostream>? I don't think it should be, should it?

Anyways, this is probably a big long question for a simple answer, but I'm pretty curious. I'm still learning C and C++, so I don't know a lot of things, and I couldn't find an answer to this particular mystery via the "Almighty" Google Machine, so here I am.

Thanks in advance!

Jasper
  • 300
  • 2
  • 11
  • 8
    iostream could include whatever header it requires. However you can not rely on this indirect include, or you code might not compile in another environment. – Rico Wang May 04 '14 at 05:23
  • 1
    So my version of `` might be different from the standard version? I'm not working on my own system, so I do not know what changes may or may not have been made to the libraries. – Jasper May 04 '14 at 05:25
  • 3
    @TheQZ: He's not saying that anything is wrong with your ``. The Standard allows it to include other headers, but it doesn't have to. If you don't directly include all the headers you use, your code can break on other compilers, where they provided an equally valid version of `` that doesn't include anything else. – Ben Voigt May 04 '14 at 05:30
  • My understanding is that the C++ standard doesn't specify how the STL lib header files should be depend on each other, so there is no standard version at all. – Rico Wang May 04 '14 at 05:30
  • 2
    Ok, makes sense. The `` for the compiler I'm using has its own `#include`s that may or may not exist in other compilers, so it's safer to `#include` the other library anyways. – Jasper May 04 '14 at 05:33
  • That is correct. You should also watch out for false friends, too. Case in point, `` defines `std::ios_base::failure`, derived from `std::system_error`, with a constructor that takes a `std::string` and a `std::error_code` by const reference. `` explicitly includes ``, so you'd think it must implicitly include `` and `` instead... but it _might_ include internal headers that define the classes themselves but not the non-member parts of their interfaces (such as, e.g., `std::string`'s `operator+()`, which is defined out-of-class)! – Justin Time - Reinstate Monica Aug 15 '23 at 19:21

1 Answers1

2

Generally, do NOT rely on the header files that includes other header files.

Always include and only include the header files you need.

For example, if you want to use std::swap(), Google it and you'll see if requires <algorithm> in c++98 and <utility> in c++11, so you should include the file to make sure your code compiles.

Mine
  • 4,123
  • 1
  • 25
  • 46