I am learning C++ right now, and at the beginning of every project my instructor puts a line that says:
using namespace std;
I understand t
But what exactly does the line tell C++ to do. What is a namespace and what is std?
std is a namespace.
A namespace is a collection of symbols.
The line tells your C++ compiler to
a) search the included parts of namespace std (i.e. iostream, iomanip) if
b) it does not find a definition of a symbol (such as (std::) cout ) before
c) declaring the symbol unknown.
Naming conflict resolution (as the other answer's describe) is probably the most important issue addressed.
What I like most about namespaces is that the namespace contents (classes and functions) can be added across multiple header files. (In contrast to a class, which has to be completely in one header file).
So, iostream, iomanip, string, stringstream each independently contribute classes and code to the std namespace.
And thus I can keep most of my tools in separate files, yet merge them 'logically' into one namespace.
For example: I have a ram based fixed size log in file dtb_log.hh, and a brief outline looks like:
// Name: dtb_log.hh
//
#ifndef DTB_LOG_HH
#define DTB_LOG_HH
namespace DTB //
{
class Log
{
public:
// ...
}; // class Log
} // namespace DTB
#endif // DTB_LOG_HH
I also have a (seldom used and in this case related) utility in file dtb_mem_tag.hh
// Name: dtb_mem_tag.hh
//
#ifndef DTB_MEM_TAG_HH
#define DTB_MEM_TAG_HH
namespace DTB //
{
class MemTag
{
public:
// ...
}; // class MemTag
} // namespace DTB
#endif // DTB_MEM_TAG_HH
Using both DTB::MemTag and DTB::Log is as simple as including the respective #include header files.
And in the application where these are used, each usage of either can be clearly marked with the 'DTB::' prefix.
Any DTB code 'evolution' needing work is in my 'dtb' namespace directory.
Thus, by file date stamps alone I can tell where the transition to ubuntu 64 has had impacts to my tools. For instance, I can see that dtb_termselect.xx (for arduino interaction code) has been updated, as has dtb_popen.xx, dtb_pause, and several others.
In a team environment (a while back), we discussed various ways to share namespaces. Generally, this was not well received ... too difficult to agree how.
But among the ideas discussed, was the idea of using the namespace as a contributing author's signature. Thus was created DTB (mine), and KTS (Ken's Transition Service), and a few others. Not everyone was enthused about this, either.