2

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.

5
  • 1
    Each header file declares the same namespace e.g. namespace std. The contents are merged together. It's purely a mechanism for reducing clutter (qualified name lookup). Commented Apr 30, 2019 at 21:35
  • There must be std:: in <string> but those files are implementation defined. Header files and namespaces are unrelated. Include just pastes the code. Commented Apr 30, 2019 at 21:35
  • I want someone to tell me how stoi is made a member of std. I can understand macros and if i open that file(string) , i can't see where it happpend. Commented Apr 30, 2019 at 21:36
  • 1
    Namespaces can span multiple files. Odds are you can't find the namespace std because it's in a file included by <string>. For example in g++'s library implementation <string> includes <bits/basic_string.h> which declares namespace std and stoi inside the std namespace. Commented Apr 30, 2019 at 21:39
  • 2
    If it helps, remember that when you #include a 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. Commented Apr 30, 2019 at 21:51

2 Answers 2

4

Namespaces can be added to. For example, in one file you might have:

  namespace myspace {
      int f();
  }

and in another:

  namespace myspace {
      void g();
  }

now both f anf g are functions in the namespace myspace. You could also provide the definitions of the functions in the above code, if you wished. Or in an implementation file you could do:

  namespace myspace {
      int f() {
           return 42;
      }
  }
Sign up to request clarification or add additional context in comments.

Comments

2

In Visual studio the <string> header has this line:

_STD_BEGIN

it is a macro defined as:

#define _STD_BEGIN  namespace std {

So the namespace is specified in the header file. Then way down on line 179 or so we find stoi:

inline int stoi(const string& _Str, size_t *_Idx = nullptr,

and further down still:

_STD_END

which expands to:

}

at the end of the std namespace.

1 Comment

Note: Identifiers that begin with an underscore followed by a capital letter (like _STD_BEGIN and _STD_END) are reserved. Don't use them in your own code, unless you're writing a compiler or standard library implementation and you know what you're doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.