The use of using

Author: Su Bingwen
Link: https://subingwen.cn/cpp/using/

In C++, using is used to declare namespaces, and using namespaces can also prevent naming conflicts. After the namespace is declared in the program, the defined classes in the namespace can be used directly. In C++11, usingnew functions are given to C++make it younger and more flexible

1. Define an alias

In C++, you can typedef redefine a type with the syntax as follows:

typedef 旧的类型名 新的类型名;
// 使用举例
typedef unsigned int uint_t;

The type being redefined is not a new type, 仅仅只是原有的类型取了一个新的名字. Like the previous declaration statement, the declarator here can also contain type modification, so that composite types can also be constructed from basic data types. A new method is specified in C++11, using alias declaration (alias declaration) to define the alias of the type, that is, using.

When used, the keyword usingbegins an alias declaration, followed by the alias and an equals sign, 其作用是把等号左侧的名字规定成等号右侧类型的别名. Type aliases are equivalent to type names, and type aliases can be used wherever a type name can appear. 使用typedef定义的别名和使用using定义的别名在语义上是等效的.

The syntax for defining an alias using using is as follows:

using 新的类型 = 旧的类型;
// 使用举例
using uint_t = unsigned int;

Through the grammatical format of using and typedef, we can see that there is not much difference in the use of the two. Suppose we define a function pointer, the advantages of using can be highlighted. Take a look at the following example:

// 使用typedef定义函数指针
typedef int(*func_ptr)(int, double);

// 使用using定义函数指针
using func_ptr1 = int(*)(int, double);

If you are not particularly familiar with function pointers and typedefs, it is difficult to see at first glance that func_ptr is actually an alias, and its essence is one. The 函数指针return type of the function pointed to is int, and the function parameters have two types of int and double.

The way of writing using 定义函数指针the alias 非常直观looks fine, the name of the alias is forcibly separated to the left, and the actual type corresponding to the alias is placed on the right, which is clearer 可读性比较好.

2. Template aliases

It is very convenient to use typedef to redefine, but it has some limitations. For example 无法重定义一个模板, we need a map with a fixed key of int type, which can be mapped to many types of value values. It is very troublesome to use typedef to define directly:

typedef map<int, string> m1;
typedef map<int, int> m2;
typedef map<int, double> m3;

In this case we unconsciously think of templates:

template <typename T>
typedef map<int, T> type;	// error, 语法错误

Using typename does not support defining aliases for templates. This simple requirement is difficult to achieve only through typedef. You need to add an external class:

#include <iostream>
#include <functional>
#include <map>
using namespace std;

template <typename T>
// 定义外敷类
struct MyMap
{
    
    
    typedef map<int, T> type;
};

int main(void)
{
    
    
    MyMap<string>::type m;
    m.insert(make_pair(1, "luffy"));
    m.insert(make_pair(2, "ace"));

    MyMap<int>::type m1;
    m1.insert(1, 100);
    m1.insert(2, 200);

    return 0;
}

From the above example, it can be intuitively felt that the requirement is simple but it is not easy to implement. In C++11, a new feature is added that can be used using 来为一个模板定义别名. The above requirements can be written as follows:

template <typename T>
using mymap = map<int, T>;

The complete sample code is as follows:

#include <iostream>
#include <functional>
#include <map>
using namespace std;

template <typename T>
using mymap = map<int, T>;

int main(void)
{
    
    
    // map的value指定为string类型
    mymap<string> m;
    m.insert(make_pair(1, "luffy"));
    m.insert(make_pair(2, "ace"));

    // map的value指定为int类型
    mymap<int> m1;
    m1.insert(1, 100);
    m1.insert(2, 200);

    return 0;
}

In the above example, by using using to assign an alias to the template, it is very convenient to assign the corresponding type to the value based on the alias, so that the written program becomes more flexible and looks more concise.

Finally, I want to emphasize one point: the using syntax is the same as typedef, it does not create new types, they just define new aliases for certain types. using 相较于 typedef 的优势在于定义函数指针别名时看起来更加直观,并且可以给模板定义别名.

Guess you like

Origin blog.csdn.net/weixin_38346042/article/details/131364900