I am having trouble with header files and namespaces. If we define functions in the namespaces then we can use the name of that namespace as identifier.
This is ok but what is really confusing that most of the functions in C++(which are declared in different header files) are in the std namespace.
Now is the namespace inside the header file or header file inside the namespace?
If the namespace is inside each header file then how can you make a function(which is declared and fully implemented in header file) member of that predefined namespace?
A concrete example:
When i try to use string in C++ then i use #include <string> but then i have to use the identifier to call a function(like std ::stoi).
Now when i open the header file string i see that stoi is fully implemented in the header file string. So how is this function a member of std namespace because in string header file i don't see std anywhere nor it is made a member of std namespace explicitly.
My Question can be very basic but i don't find answer anywhere. Simply declaring a function in namespace and then using it is simple but a function which is declared and implemented in header file, how is that function a member of any namespace and how can one namespace have functions from different header files?
It would be enough if someone tells me how stoi is made a member of std. The rest i can do by myself.
namespace std. The contents are merged together. It's purely a mechanism for reducing clutter (qualified name lookup).stoiis made a member ofstd. I can understand macros and if i open that file(string) , i can't see where it happpend.namespace stdbecause it's in a file included by<string>. For example in g++'s library implementation<string>includes<bits/basic_string.h>which declaresnamespace stdandstoiinside thestdnamespace.#includea file, the result is that that file is literally copied-and-pasted into the source code. So there's no real difference between using multiple files with versus a single file, when you have the same namespace appearing multiple times.