How to systematically understand namespace in C++

How to systematically understand the namespace in C++, what namespaces are in TensorRT

Reference & amp; COpy

Detailed understanding of namespace (using namespace) in C++_c language using namespace_Futong’s blog-CSDN blog

Background:

1. There are certain restrictions on the scope of variables and functions; for example, a temporary variable defined in the function body cannot be used outside the function body.

2. Three levels of scope are defined in the C language, namely files (compilation units), functions and compound statements. C++ introduced class scope, and classes appear within the file. Variables with the same name can be defined in different scopes without disturbing each other, and the system can distinguish them.

Problem to be solved:

1. In order to solve the scope of variables and functions, the concept of namespace was introduced in the C++ language, and the keywords namespace and using were added.

Basic concept:

1. A group of variables and functions can be defined in a namespace. These variables and functions have the same scope. These can be defined >Variables and functions are called members of this namespace.

2. namespace c=car; //Define the alias of the namespace

3. int Time; // External variables belong to the global namespace global namespace;

4. // width=2; // Namespace should be specified for non-global variables and currently valid temporary variables

5. // Nesting of namespaces

#include <conio.h>
#include <iostream.h>
namespace car // Definition of namespace
{
  int model;
  int length;
  int width;
}

namespace plane
{
  int model;
  namespace size // Nesting of namespaces
  {
    int length;
    int width;
  }
}

namespace car // Add members of the namespace
{
  char * name;
}

namespace c=car; // define the alias of the namespace
int Time; // External variables belong to the global namespace

void main()
{
  car::length=3;
  //The following sentence is wrong, so it is blocked.
  // width=2; // Namespace should be specified for non-global variables and currently valid temporary variables
  plane::size::length=70;
  cout<<"the length of plane is "<<plane::size::length<<"m."<<endl;
  cout<<"the length of car is "<<car::length<<"m."<<endl;
  //Use namespace aliases
  cout<<"the length of c is "<<c::length<<"m."<<endl;
  int Time=1996; // Temporary variables should be distinguished from global variables
  ::Time=1997;
  cout<<"Temp Time is "<<Time<<endl;
  cout<<"Outer Time is "<<::Time<<endl;
  // use keyword using
  using namespace plane;
  model=202;
  size::length=93;
  cout<<model<<endl;
  cout<<size::length<<endl;
  getch();
}

operation result:
the length of plane is 70m.
the length of car is 3m.
the length of c is 3m.
Temp Time is 1996
Outer Time is 1997

6. Namespace: It is actually a memory area named by the programmer.

Advantages:

1. Through namespaces, you can use the same variable name or function name in the same file, as long as they belong to different namespaces.

2. In addition, namespace allows code to operate on variables with the same name but belonging to different libraries.

How is this done? Just add the namespace and it will be fine;

3. Moreover, namespace can also improve the compatibility between C language and C++ language.

I can’t understand it yet, so I’ll wait and learn the C code;

Various functions in C language programs are basically implemented by functions. During the development of C language, a function library with rich functions was established. C++ inherited this valuable wealth from C language. C language function libraries can be used in C++ programs.

If you want to use the functions in the function library, you must include the relevant header files in the program file. In different header files, different function declarations are included.

There are two ways to use these header files in C++. 1. Using the traditional method of C language, the header file name includes the suffix. h, such as stdio.h, math.h, etc. Since the C language does not have a named namespace, the header files are not stored in the named namespace, so if the suffix is used in the C++ program file. h header file, you do not need to use the namespace. Just include the used header files in the file. Such as #include

2. Use the new method of C++. The C++ standard requires that the header files provided by the system do not include suffixes. h, such as iostream, string. In order to indicate the connection and difference with the C language header file, the header file name used by C++ is preceded by the corresponding header file name of the C language (but not including the suffix .h). letter c. For example, the header file related to input and output in theC language is named stdio. hThe corresponding header file in C++ is named cstdio. The header file math.h in C language, and the corresponding header file in C++ is called cmath. Header file string in C language. hThe corresponding header file in C++ is named cstring. Note that in C++, the header file cstnng and the header file strmg are not the same file. The former provides declarations of functions (such as strcmp, ctrcpy) for string processing in C language, and the latter provides new functions for string processing in C++. In addition, Since these functions are declared in the named namespace std, the named namespace std must be declared in the program. For example: #include #include using namespace std; Most of the C++ compilation systems currently used retain the usage of c and provide new methods of C++. The following two usages are equivalent and can be optional. C traditional method C++ new method #include #include #include #include #include #include using namespace std; The traditional c method can be used, but the new method of C++ should be promoted.

Summary:

1. As can be seen from the above, the namespace defines a set of variables and functions, which have the same scope. For different namespaces, you can define the same variable name or function name. When using it, just distinguish the different namespaces before the variable name or function name.

2. Namespaces can be nested and defined. When using, members must be referenced level by level with the namespace qualifier: :.

3. The system has a global namespace by default, which contains all external variables. This namespace has no name. When referencing variables in this namespace, you must use the namespace qualifier: :, without a name in front. Without using namespaces, we know that external variables with the same name cannot be defined in different files because they belong to the same global namespace and names cannot be repeated.

4. You can give the namespace an alias. Generally an alias is a shorter name to simplify programming.

5. On the basis of the originally defined namespace, members can be added to it at any time.

Why use using

1. As you can see in the previous routine, for the convenience of use, the keyword using is introduced. The using declaration allows you to refer to namespace members without using the namespace qualifier::.

2. In addition, the use of keywords namespace and using has a certain impact on function overloading.

The following is a detailed explanation through a routine.
#include <conio.h>
#include <iostream.h>
namespace car // Definition of namespace
{
  void ShowLength(double len) //The parameter type is d o u b l e
  {
    cout<<"in car namespace: "<<len<<endl;
  }
}

namespace plane // Definition of namespace
{
  void ShowLength(int len) //The parameter type is i n t
  {
    cout<<"in plane namespace: "<<len<<endl;
  }
}

void main()
{
  using namespace car;
  ShowLength(3);
  ShowLength(3.8);
  using namespace plane;
  ShowLength(93);
  ShowLength(93.75);
  getch();
}

operation result:
in car namespace: 3
in car namespace: 3.8
in plane namespace: 93
in car namespace: 93.75

Without the interference of namespaces, the selection rules for function overloading would be very simple. As long as the actual parameter is of type double, the previous function is called; if the actual parameter is of type int, the later function is called. However, due to the participation of namespace, the above running results appear. Therefore, you must pay attention to the impact of namespace on function overloading when programming.

Points to note:

1. When calling a function, if the data types of the actual parameters and formal parameters cannot completely match, appropriate data type conversion may be performed on the actual parameters. For example, convert the char type to the int type, or further convert the int type to the double type. This is to convert the data type from simple to complex, generally without losing information.

2. Another conversion is the other way around, converting the double type to the int type, or further converting the int type to the char type. This is to convert the data type from complex to simple, and some information may be lost. When calling a function, C++ has different priorities for the above two conversions under different circumstances. When the namespace is introduced, it participates in the allocation of the above priority order. [It’s a bit abstract, there’s no way to get it completely yet].

3. After using namespace std, std:: is omitted, and functions and variables in the std space can be directly referenced.

namespace in tensorRT

https://docs.nvidia.com/deeplearning/tensorrt/api/c_api/namespaces.html

image-20230925155733944