Namespaces (namespace) are very important in C + +. The purpose of this article is not to explain the syntax of namespaces, but to demonstrate how namespaces are used, or tips for using namespaces.
Namespaces can simply encapsulate some names (name) in a different name package. Say:
namespace net {
class Socket {
...
};
}
...
net::Socket socket;
After such encapsulation, if the socket class is implemented in two libraries (the library), you can use them at the same time without naming conflicts, as long as they have different names.
But there is a problem: if both companies are writing a network library, how likely are they to use sockets to name their classes when they write code? I guess it's close to 100%.
The name of the namespace is best for easy input, which means that the name of the namespace is best not to be too long, 2-4 characters on it. With that in mind, how big are the two companies ' chances of calling their namespaces net? 5% or 10%?
It's not hard to see that namespaces don't solve all the problems, it's just that the chances of naming conflicts are relatively small.
There is a method called "industrialized length" (Industrial strength Solution), which uses a long unique name when naming namespace, and uses a short alias in a program. So the network library might look like this:
namespace net_33843894 {
class Socket {
...
};
}
The number behind the net_ is generated by a random number generator. In order to describe the convenience later, we assume that the above code is placed in the <netlib> header file.
When a user uses our library, it is necessary to write his own header file <mynetlib>, which includes the following:
#include <netlib>
namespace net = net_33843894;
He created a valid alias in this project to represent the namespaces provided to him in the library. If the name net is already in use by another library, then he can choose another name instead, such as net2,sock, or something else.
Is that all you got? Not yet. One more thing you have to do: make your library easier and easier to use. In this exquisite society, when people double-click an installation file, your library should be available in their development environment, followed by #include <netlib>, and then they will be able to do something else.
However, the situation now is that users need to create a header file of their own to use your library, although it's not a big deal, but not everyone can tolerate it. The solution is to provide a reasonable default value directly, if the user does not feel appropriate can also be canceled, so, in your header file using the Precompiled option, as follows:
namespace net_33843894 {
class Socket {
...
};
}
#ifndef NO_NET_33843894_ALIAS
namespace net = net_33843894;
#endif
This gives the name of the namespace a default value, and if the name is already used, the user can define a No_net_33843894_alias macro and the alias will be canceled.
Unfortunately, even with a short alias net, when you use the Socket class incorrectly, in the compiler I used, there is no short alias that can be displayed in the error message, but it still uses net_33843894::socket. It's a little hard to read.
What to do? Look at me.
#ifdef NO_NET_33843894_ALIAS
namespace net_33843894 {
#else
namespace net {
#endif
class Socket {
...
};
}
#ifndef NO_NET_33843894_ALIAS
namespace net_33843894 = net;
#endif
If you don't have a macro no_net_33843894_alias defined, just give the namespace a shorter name, and make the alias a little longer. In this way, the error message will be more pleasing to read.