1

I was making some c++ programme where I pushed back strings into a string vector and I accidentally removed the string library from the file (#include <string>)... absolutely nothing happened at compile or run time and the programme executed successfully. Why is that?

Aaronv
  • 63
  • 7
  • 4
    It sometimes just so happens that some standard library headers include other standard library headers, a fact that you should not rely on. Always include the headers you use and all will be well, even if it might not seem necessary. – Paul Sanders Aug 20 '20 at 10:54
  • I don't think `#include ` is required for `std::string`, see [OnlineGDB](https://onlinegdb.com/rJs28Csfv). – Rohan Bari Aug 20 '20 at 10:54
  • 2
    @RohanBari yes it is: https://en.cppreference.com/w/cpp/string/basic_string It's just included somewhere else. – Matthieu Brucher Aug 20 '20 at 10:56
  • 4
    @RohanBari: It is a requirement that you include the header associated with whatever you're using. It's just not a requirement (in C++) that it **doesn't** work if you don't. Welcome to UB world. – DevSolar Aug 20 '20 at 10:57
  • @deW1 I was using ```std::string``` @PaulSanders yea it's probably that although I don't see a reason as to why I should keep track of library-overlap @RohanBari I think you're right, it's just the string functions that the library is used for – Aaronv Aug 20 '20 at 10:58
  • @DevSolar haha oh nooooes – Aaronv Aug 20 '20 at 10:59
  • 2
    @Biaaach: In C, it is defined that any standard header you include reserves **only** the identifiers it is defined to reserve (and some that are "reserved to the implementation" in all cases anyway). This is so you can use identifiers that are in the standard somewhere as long as you don't include their header. (You need only "know" those parts of the standard you're actually using.) C++ has no such restriction on standard headers including each other, since identifiers are "hidden away" in `std::` anyway. To have a conforming program, you still need to include `` to use `std::string`. – DevSolar Aug 20 '20 at 11:01
  • 1
    There's a tool called `include-what-you-use` that can help you to detect when you've forgotten to include something (but got it "for free" via some other header). It's not perfect and sometimes suggests `C` headers instead of the `C++` versions, but it _does_ help. – Ted Lyngmo Aug 20 '20 at 11:09
  • `#include ` and `int main() { std::vector v; }` doesn't work on my machine. – Eljay Aug 20 '20 at 12:38
  • @Eljay I guess it's also machine dependent – Aaronv Aug 21 '20 at 11:36
  • 1
    You're looking at undefined behaviour. Of course it's machine dependent. ;-) – DevSolar Aug 21 '20 at 15:29

1 Answers1

3

That depends on what your standard library decides to include. You should include string, but it may already be included somewhere, perhaps for exception handling. It will also depend on your compiler and your compiler version.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • god dammit how is bjarne always one step ahead im mad! (I'm using minGW btw) – Aaronv Aug 20 '20 at 11:00
  • 1
    Typically gcc had a huge cleanup a few versions ago and reduced the crossed headers, and lots of code stopped working because some classes were not included by chance anymore. If you use something, you should always include the corresponding header, even if it's included by chance by something else. – Matthieu Brucher Aug 20 '20 at 11:02