Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to know that where are the namespaces created by us are stored ? if i want to make my own namespace then where to save it? where is the std namespace is stored ?
Posted

You don't store it anywhere specific, you just declare it, and put your classes/functions in it, like this:
C++
namespace YourNameSpace
{
   class SomeClass { };

   void SomeFunction() { }
}
 
Share this answer
 
v2
Note that namespace is a packaging concept, not a type. That is, one cannot create instances of a namespace. A namespace is a sophisticated mechanism for packaging libraries. If all the internals of a library are accessible, the packaging is no more than a bag of items. Generally, there are a few items that a library intends to present to the outside world, and everything else is for its own internal use. The few items to be exported cause much less name clash than exposing the entire namespace. All names other than operator delete and operator new in the C++ library headers are defined in the std namespace, or in a namespace nested within the std namespace. Including a C++ library header does not introduce any library names into the current namespace. You refer to the name cin, for example, as std::cin. Alternatively, you can write the declaration:
C++
using namespace std;

(A using declaration allows us to access a name from a namespace without the prefix namespace_name:: )

Include the standard header <cstdlib> to effectively include the standard header <stdlib.h> within the std namespace.
C++
#include <stdlib.h>

namespace std {
using ::size_t; using ::div_t; using ::ldiv_t;
using ::abort; using ::abs; using ::atexit;
using ::atof; using ::atoi; using ::atol;
using ::bsearch; using ::calloc; using ::div;
using ::exit; using ::free; using ::getenv;
using ::labs; using ::ldiv; using ::malloc;
using ::mblen; using ::mbstowcs; using ::mbtowc;
using ::qsort; using ::rand; using ::realloc;
using ::srand; using ::strtod; using ::strtol;
using ::strtoul; using ::system;
using ::wcstombs; using ::wctomb;
#if __IBMCPP_TR1__
namespace tr1 {
using ::lldiv_t; using ::llabs; using ::lldiv;
using ::atoll; using ::strtoll; using ::strtoull;
using ::strtof; using ::strtold;
}
#endif
}

There is one case in which we should always use the fully qualified library names: inside header files. The reason is that the contents of a header are copied into our program text by the preprocessor. When we #include a file, it is as if the exact text of the header is part of our file.
If we place a using declaration within a header, it is equivalent to placing the same using declaration in every program that includes the header whether that program wants the using declaration or not .In general, it is good practice for headers to define only what is strictly necessary.
 
Share this answer
 
You probably meant to ask: How are namespaces stored in the output files the compiler and linker generate? And that is in fact an interesting question.

Namespaces often serve as a tool to allow multiple functions with the same name to reside inside the same program, without getting into way. And hence all those functions must be identified somehow by the linker in a unique way. That's what namespaces are all about.

So, the answer to your question is: The compiler adds the name of the namespace to the function's name by pre- or postfixing that name. By the way, it does the same thing to distinguish multiple versions of a function that differ in their arguments -- also called overloading.

Here is an example. In my source code I had:
namespace MyNamespace
{
    int MyFunction (const char* pIn)
    {
        return 5;
    }
}


Now I set the project settings so that the linker produces a link map. In that map I find the above function under the name:

?MyFunction@MyNamespace@@YAHPBD@Z


and that's how another program unit can find MyFunction in namespace MyNamespace, by reconstructing that very "mangeled" name. The YAHPBD@Z is the mumbo jumbo the compiler uses to encode the number and type of arguments of the function.

Note that the methode of pre- or postfixing the function name is dependent on the compiler implementation. So it is nothing to rely on for a life time. The compiler vendor just guarantees what is in the language definition, namely that your namespaces are nicely separated.
 
Share this answer
 
Comments
[no name] 16-Jul-12 20:35pm    
Not quite the OPs question I think but interesting all the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900