Namespace after include

I have a quick question, I have a background in programming but its through Java, and web development.
Why is it that after we have imported the library of code with our #include statement that we then have to specify which namespace is being used for std? Wouldn’t it make more sense to just have to import the code with the #include statement and then be able to use the code we imported, without having to access the namespace?
I understand that the code is probably broken up into several namespaces within iostream, and we need a way to access that code, but why don’t we import just the namespace thats needed in our import statement instead of having to break it apart manually later. Thanks for the help!

I think the problem is: what if the std namespace internally uses aspects of iostream that are not within the std namespace? If that’s so, then you cannot simply include the namespace because it wouldn’t have access to the code it needs to function correctly.

In case there’s some confusion about what the directives are doing, ‘include’ is necessary and inserts code into the file, while ‘using’ is simply a quality-of-life directive that makes accessing the contents of the code easier (with caveats like potential name clashes).

You can read more on this stackoverflow thread that covers the same question:

Thanks Jon, that answered a few things. Still wasn’t quite 100% understanding but after watching the next couple of videos in this series, and doing some searching of my own, I’m beginning to understand it a lot more now.

Privacy & Terms