-4

All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the std namespace.

Namespaces

Now if everything is 'declared' in std namespace they why are these fancy headers out there?

Have already checked this but it was not helpful enough.

Community
  • 1
  • 1
stillanoob
  • 1,279
  • 2
  • 16
  • 29
  • 5
    The two have nothing whatsoever to do with one another... Your question is meaningless. – Lightness Races in Orbit Nov 22 '15 at 03:14
  • @LightnessRacesinOrbit Then why the declarations again in std namespace when they are already there in headers? – stillanoob Nov 22 '15 at 03:20
  • That doesn't make any sense. The declarations are lexically "in" the header. That's where they live. It's where somebody typed them and it's the file they're found in by your compiler when searching for them. The namespace is a semantic categorisation, used _after_ your headers have been included to sort out everything in your program (from headers or otherwise). A namespace is really just part of the name of the thing you're declaring; that's it. I really don't see what you're getting at. It's like asking "why do we need different HTML tags when they're all already in an HTML file?" – Lightness Races in Orbit Nov 22 '15 at 03:33
  • @LightnessRacesinOrbit Okay I am sorry. Since string class is declared in `` header and 'also' in std namespace, can you please give some indication as of how the declaration would look like in std namespace? – stillanoob Nov 22 '15 at 11:49
  • Having written my answer, I now think that your question is an exact duplicate of the question you linked to. What was "not helpful enough" about it? – Lightness Races in Orbit Nov 22 '15 at 14:51

4 Answers4

1

Namespaces are kind of a way to organize our types. Keep all your math functions in the MyMath namespace, etc. It's also a way to separate out your types so that they don't clash with other types. So you can have both a MyTypes::string and an stl::string in your code. The std namespace is the one the STL has chosen for its stuff.

Header files contain the public interface of code. It gives you what it has available, which types and functions it declares and hopefully documentation in comments about how to use it. If you try to use code without including its corresponding header file, your code won't compile because it can't find the types. Headers may or may not contain code in namespaces.

djcouchycouch
  • 12,724
  • 13
  • 69
  • 108
  • Header files contain declaration so do namespaces, then isn't it some sort of re-declaration? – stillanoob Nov 22 '15 at 03:15
  • @ibrahim5253 the namespace is **part** of the declaration. A function call `foo` is a different function from `std::foo` (the second one is a function within the namespace `std`). The namespace makes it easier to reduce name clashes. – Anon Mail Nov 22 '15 at 03:20
  • @ibrahim5253: _"so do namespaces"_ No, not really. Perhaps you could give a graphical description of what you think happens here? – Lightness Races in Orbit Nov 22 '15 at 03:35
1

As you may know, When you include something like #include "a.h" in b.cpp, everything inside a.h will be placed instead of #include "a.h" in b.cpp. So if a.h is 200 lines of code and your actual code is 10 lines of code, the code that will be compiled would be about 210 lines code. And compile time will be increased if your a.h is big or you included it several times.( Pay attention that if something is included inside of a.h this story repeats. )
Let's suppose that std library is inside one .h file and inside std namespace. Now it's really big and In each file that you are going to use one of std classes, even the smallest one, you have to include whole of it. It makes your program really big and the compile really slow.

s4eed
  • 7,173
  • 9
  • 67
  • 104
0

The snippet you have is a requirement. The header files are where the standard library entities are actually declared. There is more than one header due to the size and diversity of the entities contained in the standard library. Essentially, the declaration are grouped according to functionality.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
-1

Headers and namespaces are effectively unrelated; one does not have anything to do with the other.

As such, one also does not make the other obsolete in any way.

Consider this declaration of std::string that you might find in <string>:

namespace std {
   typedef basic_string<char> string;
}
  • The namespace is a categorisation for the names created (and used) by the declaration;
  • The header file is a categorisation for where to store the declaration on your hard disk.

There's no "redeclaration" whatsoever because the two concepts are unrelated.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055