# Where is the namespace 'std' declared

## Recommended Posts

I was wondering if anyone knew where I could find the declaration namespace std. I was wanting to view it. I assumed it would be declared in the iostream header because that is the only thing I include when I use it a lot of times. I went there to view it but did not see any declaration.

##### Share on other sites
It is declared in every header of the standard c++ library.

But note that the standard does not describe how those headers actually look like, they might not even be valid c++. Implementors might use a lot of #define-magick, or they might even choose to make intrinsics (types that are built into the compiler), in which case the header files might not even physically be there at all.

But just from the meaning, std is declared in them.

##### Share on other sites
It's in all of the standard headers (as they all put stuff into the std namespace).

If you're using MSVC, you'll see it declared through the macro _STD_BEGIN (which is defined as namespace std {)

##### Share on other sites
Unlike classes and structs, namespaces can be added to at any time. So each time you add a header from the standard library, it appends to the std namespace. If you type std::, your compiler auto-complete will only show elements that have been added be the various headers and their dependencies.

##### Share on other sites
Ah, all good information. So how could I set up my own namespace to encompass this type of behavior? Where I could have my own header files that add to a namespace as they are added to a file.

Would I have to use the so-called "define magik" that was mentioned earlier in the thread?

I see a couple define macros in the iostream header, but I can't really make heads or tails from it. Anyone know where I should start if I want to emulate this?

##### Share on other sites
Quote:
 Original post by CoryG89Ah, all good information. So how could I set up my own namespace to encompass this type of behavior? Where I could have my own header files that add to a namespace as they are added to a file.

namespace myfoo {  // stuff};

##### Share on other sites
Quote:
 Original post by Antheusnamespace myfoo { // stuff};

Yeah, I realized that part. I guess I could be clearer about what I mean. If I create different header files, such as the different ones that make up the C++ Standard Library. And as I understand it, each of these header files add to the same namespace std.

So would I be able to just declare the same-named namespace in all the header files. Each one adding different functions and what not. And then, include only the header files I need in my program and I can use the same syntax like

using namespace examplenamehere

The only reason I wasn't sure of this is because nowhere in any of the header files of the standard library that I have examined have any namespaces directly declared within them like this.

##### Share on other sites
Yes.

Quote:
 Original post by CoryG89The only reason I wasn't sure of this is because nowhere in any of the header files of the standard library that I have examined have any namespaces directly declared within them like this.

This was the point made earlier. There's a preprocessor definition "_STD_BEGIN (which is defined as namespace std {)". The preprocessor will just blindly look for any occurrences of '_STD_BEGIN' in the source file and replace it with 'namespace std {'.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978519

• 10
• 10
• 12
• 22
• 13