Namespace plays an extraordinary role in C ++. The purpose of this article is not to elaborate the syntax of the namespace, but to demonstrate how to use the namespace, or to use the namespace tips.
A namespace can simply package some names with another name. For example:
namespace net {
class Socket {
...
};
}
...
net::Socket socket;After such encapsulation, if both libraries implement socket classes, as long as their namespaces have different names, you can use them at the same time without any naming conflicts.
But there is still a problem: if both companies need to write a network library, how likely is it to use socket to name their classes when writing code? I guess it's close to 100%.
The namespace name should be easy to enter. That is to say, the namespace name should not be too long. It can be 2-4 characters long. With this idea, what are the opportunities for the two companies to call their namespace "Net? 5% or 10%?
It is not hard to see that the namespace does not solve all the problems, but it only makes the chance of a name conflict relatively small.
There is a method called "Industrial length" (industrial strength solution), which uses a unique long name when naming namespace, while using a short alias in a program. The network library may look like this:
namespace net_33843894 {
class Socket {
...
};
}Net _ is generated by a random number generator. For future convenience, we assume that the above Code is in the header file <netlib>.
When users use our library, they need to write their own header file <mynetlib>, which includes the following content:
#include <netlib>
namespace net = net_33843894;He created a valid alias in this project to represent the namespace provided to his library. If the name net has been used by another library, you can also select another name, such as net2, Sock, or something else.
In this case, is everything okay? No. You also need to do one thing: Make your database easier and more convenient to use. In this perfect society, after people double-click an installation file, your library should be available in their development environment. The next step is # include <netlib>, then they can do something else.
However, the current situation is that users need to create their own header files to use your library. Although this is no big deal, not every user can tolerate this. The solution is to directly provide a reasonable default value. If the user finds it inappropriate, it can be canceled. Therefore, use the pre-compilation option in your header file, as shown below:
namespace net_33843894 {
class Socket {
...
};
}
#ifndef NO_NET_33843894_ALIAS
namespace net = net_33843894;
#endifIn this way, we provide a default value for the namespace name. If this name is already used, you can define a no_net_33843894_alias macro and the alias will be canceled.
Unfortunately, even if the short alias net is used, when the method of using the socket class is incorrect, in the compiler I have used, no short alias can be displayed in the error message, but still uses net_33843894: socket. Reading is difficult.
What should I do? View me.
#ifdef NO_NET_33843894_ALIAS
namespace net_33843894 {
#else
namespace net {
#endif
class Socket {
...
};
}
#ifndef NO_NET_33843894_ALIAS
namespace net_33843894 = net;
#endifIf the macro no_net_33843894_alias is not defined, a shorter name is given to the namespace and the alias can be extended. In this way, the error message will become more pleasing to the eye.