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 that it keeps you from having to call functions in headers you include with their header name like iostream::stdout and instead just call stdout.
But what exactly does the line tell C++ to do. What is a namespace and what is std?
I am also new to programming besides python so switching to a new paradigm is very confusing for me.
From cppreference.com:
Namespaces provide a method for preventing name conflicts in large projects.
Symbols declared inside a namespace block are placed in a named scope that prevents them from being mistaken for identically-named symbols in other scopes.
Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.
A namespace works to avoid names conflicts, for example the standard library defines sort()
but that is a really good name for a sorting function, thanks to namespaces you can define your own sort()
because it won't be in the same namespace as the standard one.
The using directive tells the compiler to use that namespace in the current scope so you can do
int f(){
std::cout << "out!" << std::endl;
}
or:
int f(){
using namespace std;
cout << "out!" << endl;
}
it's handy when you're using a lot of things from another namespace.
source: Namespaces - cppreference.com
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 // <acronym description>
{
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 // <acronym name def>
{
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With