Example of how to use the namespace namespace in C++

  • 2020-06-01 10:31:43
  • OfStack

The namespace

In C++, names (name) can be symbolic constants, variables, macros, functions, structures, enums, classes, objects, and so on. To avoid naming conflicts for these identifiers in large-scale program design and when programmers use a variety of C++ libraries, the standard C++ introduces the keyword namespace (namespace/namespace/namespace/name field) to better control the scope of identifiers.

Namespaces are not used in MFC, but are heavily used in the.NET framework, MC++, and C++/CLI.

We often see statements like this:


using namespace std;

Or there's this:


using std::string;

These are essentially namespace techniques using C++.

What is a namespace? A namespace can be thought of as a declared region in which variables are valid internally, for example, if there are two namespaces:


namespace Jack {
 double pail;
 void fetch();
}

namespace Jill {
 double pail;
 void fetch();
}

The two namespaces have one of the same variables, but because they are in different namespaces, they do not conflict with each other. When we use it, we can call:


Jack::pail = 12.34;
Jill::pail = 56.78;
Jack::fetch();

So, we sometimes see code that looks like this:


int x;
std::cin >> x;
std::cout << x << std::endl;

But if we had used the using declaration, it would have been 1 point easier:


using std::cin;
using std::cout;
using std::endl;
int x;
cin >> x;
cout << x << endl;

Or, more simply, we can compile the instructions using using:


using namespace std;
int x;
cin >> x;
cout << x << endl;

Among them, cin, cout, and endl all exist in the std namespace.

Above are actually two types of using, one called using declaration and one called using compile instructions. The using declaration is used to declare a variable or function in a namespace, while the using compilation directive declares the entire namespace.

In general, we recommend that you use using declarations for the required variables or functions, so that you don't have to declare all of them. Some variables or functions may also exist in other namespaces, which will cause conflicts. In fact, this is not allowed.

Namespaces can also be nested.

Some namespaces have no names:


namespace {
 int ice;
 int water;
}

This namespace does not have a name, so it cannot be used with the using declaration and the using compilation instructions. It cannot be used with other files, but only at the current file declaration and at the end of the scope, which is similar to static. In fact, C++ does not recommend using static, but recommends it:


static int counts;//  Is not recommended 
namespace {
 int counts;//  recommended 
}
int main() {
 ...
}

The most common use of namespaces is for other files to access. It's like std1.

There are also some guidelines for the use of namespaces:

Use variables declared in the named namespace instead of external or static global variables. If you develop a function library or class library, place it in one namespace. Don't overuse the using compilation instructions. Do not compile instructions using using in header files for unknown effects. When importing a name, the using declaration is preferred. For the using declaration, it is preferred to scope it locally rather than globally.

conclusion


Related articles: