Sign in to follow this  
CoryG89

Where is the namespace 'std' declared

Recommended Posts

CoryG89    123
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 this post


Link to post
Share on other sites
phresnel    953
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 this post


Link to post
Share on other sites
taz0010    277
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 this post


Link to post
Share on other sites
CoryG89    123
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 this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by CoryG89
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.


namespace myfoo {
// stuff
};

Share this post


Link to post
Share on other sites
CoryG89    123
Quote:
Original post by Antheus

namespace 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 this post


Link to post
Share on other sites
TheUnbeliever    963
Yes.

Quote:
Original post by CoryG89
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.


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 {'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this