简体   繁体   English

在C ++中使用“using”关键字

[英]“using” keyword in C++

I am learning C++. 我正在学习C ++。 And my professor uses some code which is something like 我的教授使用了一些类似的代码

using filePath = std::string;
using setOfPaths = std::set<filePath>;
using iterOfSet = setOfPaths::iterator;
using listOfIter = std::list<iterOfSet>;
using iterList = listOfIter::iterator;
using fileName = std::string;
using mapOfFileName = std::map<fileName, listOfIter>;
using iterOfMap = mapOfFileName::iterator;

setOfPaths _setOfPaths;
mapOfFileName _mapOfFileName;
iterOfSet setIter;

I want to know why we are using the using keyword. 我想知道为什么我们使用using关键字。 Why can't we simply write 为什么我们不能简单地写

std::string filepath;

std::set<filepath> setOfPaths;
...
...

What is the benefit of having using keyword? using关键字有什么好处?

The using keyword is used to define type aliases. using关键字用于定义类型别名。 The reasons your professor is using it are: 你的教授使用它的原因是:

  • readability 可读性
  • being more descriptive 更具描述性
  • avoid unnecessary typename 避免不必要的typename

Readability and descriptiveness 可读性和描述性

You can use type aliases to semantically (and only that) restrict a specific type, making the name more descriptive for the specific use. 您可以使用类型别名来语义(并且仅限于此)限制特定类型,使名称对特定用途更具描述性。

An example is: 一个例子是:

using fileName = std::string;

The fileName alias is used to describe a file name string, not just any string. fileName别名用于描述文件名字符串,而不仅仅是任何字符串。 This makes for readable function signatures too. 这也使得可读功能签名成为可能。

I feel like I have to iterate this again: it's simply an alias. 我觉得我必须再次迭代这个:它只是一个别名。 Any function taking fileName as an argument will work just fine with any std::string argument. fileName作为参数的任何函数都可以与任何std::string参数一起使用。

Unnecessary typename s 不必要的typename

Some may seem unnecessary, like: 有些似乎是不必要的,如:

using setOfPaths = std::set<filePath>;

but in some cases they can be actually used to avoid having to specify typename in situations like: 但在某些情况下,它们实际上可用于避免在以下情况下指定typename

template<typename Type>
struct something {
    using something_iter = typename std::set<Type>::iterator;
};

with: 有:

template<typename Container>
using itertype = typename Container::iterator;

template<typename Type>
struct something {
    using something_iter = itertype<std::set<Type>>;
}; 

By moving typename in a specific alias we can reuse itertype in multiple other occasions effectively avoiding typename . 通过在特定别名中移动typename ,我们可以在多个其他场合重用itertype ,从而有效地避免使用typename

A note on typedef 关于typedef

There's another way to define type aliases: typedef . 还有另一种定义类型别名的方法: typedef That keyword is inherited from C and does not allow for templated aliases, like: 该关键字继承自C,不允许模板化别名,例如:

template<typename Type>
using vec = std::vector<Type>;

A note on type safety 关于类型安全的说明

This is not actually any more type safe than not using aliases at all. 这实际上不比不使用别名更安全。 Again, fileName and std::string are exactly the same type. 同样, fileNamestd::string的类型完全相同。 You can use both interchangeably. 您可以互换使用。

A possible next step would be to define a specific fileName class/struct type with its own specific invariants. 可能的下一步是定义具有其自己的特定不变量的特定fileName类/结构类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM