0

So I am learning C++ at the moment, and at some point I saw that you "have to" #include <string> at the top of your code to use strings.But when I run my code, it does the same thing in both cases.So is it really needed to use it?

#include <iostream>
using namespace std;

int main() 
{
    string fullName;
    cout << "Type your full name: "; 
    getline(cin, fullName);
    cout << "Your name is: " << fullName;

}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
StorMy
  • 137
  • 2
  • 3
  • 11
  • 2
    Please show your code. – bereal Aug 13 '19 at 16:30
  • 1
    The short answer is yes, you do need to include the header. My guess is that you are including another header that itself includes the `string` one. Without seeing your code, we can't give you a definite answer. – pcarter Aug 13 '19 at 16:33

4 Answers4

6

Some compiler implementations include the header <string> in the header <iostream>.

But you shall not rely on this.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

A compiler is not required to accept your program if you don't.

It might (coincidentally) be included through a different header and work anyway, but that might not be the case after your next compiler update or if you move to different platform.

Whether you "have to" include it is mostly down to how strict an environment you're working in – if you're hacking away at home you can do whatever, but if you're working for somebody else you're usually expected to Do The Right Thing.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 1
    What is the meaning of "A compiler is not required to accept your program if you don't"? – Brandon Dyer Aug 13 '19 at 16:49
  • 1
    I got `StorMy.cpp:6:5: error: unknown type name 'string'` when I try to compile the code. That is an example of a compiler not accepting the program. (I can fix it by adding `using namespace std::__private;` but that kind of thwarts the purpose of the `__private` namespace.) – Eljay Aug 13 '19 at 16:58
  • 1
    @BrandonDyer That it is allowed to, for instance by way of the reason mentioned in the second paragraph, but it doesn't have to. – molbdnilo Aug 13 '19 at 17:14
  • @Eljay That's what I would like `g++` and `clang++` to report too since I often forget `` for some reason. Which compiler are you using? – Ted Lyngmo Aug 13 '19 at 17:20
  • 1
    @TedLyngmo • I'm using my hacked up version of `clang++` that I've added a bunch of instrumentation. Makes for slow programs. My hacked up headers have only the symbols they need to provide, and nothing beyond (so lots of failures over un-required symbols not present in the global namespace). Over time has become less and less useful with things like `clang-iwyu` and `clang-format` and better `-fsanitize` support. Separate heaps for `new`, `new[]`, and `malloc`. – Eljay Aug 13 '19 at 17:27
  • 1
    @Eljay Darn ... Ambitious! :-) – Ted Lyngmo Aug 13 '19 at 17:31
0

Don't rely on transitive includes. Your implementation may include whatever it wants in other headers that may cause your code to work, but you cannot rely on that. A good, portable, robust program includes what it needs, when it needs it, explicitly. That way it stays working when you change compiler, standard library, Operating System, etc.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
-1

In C++, #include just says to paste the code from the file included into that location. If something is already #includeing string, then the compiler won't care if anything after it includes it.

This does not mean you should do that! Always include what you need. Don't rely on other headers, especially those from libraries, including anything for you.

Brandon Dyer
  • 1,316
  • 12
  • 21