“使用” C ++中的关键字 [英] "using" keyword in C++

查看:150
本文介绍了“使用” C ++中的关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++。我的教授使用了一些代码,例如使用filePath = std :: string;

I am learning 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;

我想知道为什么我们使用 c>关键字。为什么我们不能简单地写

I want to know why we are using the using keyword. Why can't we simply write

std::string filepath;

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

拥有 using 关键字?

推荐答案

使用关键字用于定义类型别名。您的教授使用它的原因是:

The using keyword is used to define type aliases. The reasons your professor is using it are:


  • 可读性

  • 更具描述性

  • 避免不必要的 typename

  • readability
  • being more descriptive
  • avoid unnecessary typename

您可以使用类型别名在语义上(并且仅此)限制特定类型,从而使名称对特定用途更具描述性。

You can use type aliases to semantically (and only that) restrict a specific type, making the name more descriptive for the specific use.

一个例子是:

using fileName = std::string;

fileName 别名用于描述文件名字符串,而不仅仅是任何字符串。

The fileName alias is used to describe a file name string, not just any string. This makes for readable function signatures too.

我觉得我必须再次迭代:它只是一个别名。任何以 fileName 作为参数的函数都可以与任何 std :: string 参数一起使用。

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.

有些似乎不必要,例如:

Some may seem unnecessary, like:

using setOfPaths = std::set<filePath>;

,但是在某些情况下,它们实际上可以用来避免指定 typename 在以下情况下:

but in some cases they can be actually used to avoid having to specify typename in situations like:

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

具有:

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

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

通过在特定别名中移动 typename 可以在其他多种情况下重用 itertype ,从而有效避免 typename

By moving typename in a specific alias we can reuse itertype in multiple other occasions effectively avoiding typename.

还有另一种定义类型别名的方法: typedef 。该关键字从C继承,不允许使用模板别名,例如:

There's another way to define type aliases: typedef. That keyword is inherited from C and does not allow for templated aliases, like:

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



关于类型安全的说明



此实际上,与根本不使用别名相比,类型安全性更高。同样, fileName std :: string 是完全相同的类型。可以互换使用。

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. You can use both interchangeably.

下一步可能是定义特定的 fileName 类/结构类型,它自己的特定不变量。

A possible next step would be to define a specific fileName class/struct type with its own specific invariants.

这篇关于“使用” C ++中的关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆