028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

C++中怎么使用auto避免多余的类型名重复

今天就跟大家聊聊有关C++中怎么使用auto避免多余的类型名重复,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联公司是一家专业从事成都网站制作、网站建设、外贸网站建设、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联公司依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!

Reason(原因)

Example(示例)

Consider:

考虑以下代码:

auto p = v.begin();   // vector::iterator
auto h = t.future();
auto q = make_unique(s);
auto f = [](int x){ return x + 10; };

In each case, we save writing a longish, hard-to-remember type that the compiler already knows but a programmer could get wrong.

无论哪种情况,我们都不必编写又长、类型又难记的类型信息。其实这些信息编译器已经知道,但程序员还是会弄错。

Example(示例)

template
auto Container::first() -> Iterator;   // Container::Iterator
Exception(例外)

Avoid auto for initializer lists and in cases where you know exactly which type you want and where an initializer might require conversion.

对于你确切地知道所需类型但初始化器可能需要转换的情况,应避免为初始化列表使用auto。

Example(示例)

auto lst = { 1, 2, 3 };   // lst is an initializer list
auto x{1};   // x is an int (in C++17; initializer_list in C++11)
Note(注意)

When concepts become available, we can (and should) be more specific about the type we are deducing:

在concepts可以之后,我们可以(也应该)更加明确我们推断的类型。

// ...
ForwardIterator p = algo(x, y, z);
Example (C++17)

示例(C++17)

auto [ quotient, remainder ] = div(123456, 73);   // break out the members of the div_t result
Enforcement(实施建议)

Flag redundant repetition of type names in a declaration.

标记在声明时发生的多余的类型名称重复。

看完上述内容,你们对C++中怎么使用auto避免多余的类型名重复有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


本文题目:C++中怎么使用auto避免多余的类型名重复
网址分享:http://www.tsicrk.com/article/gisgdc.html

其他资讯

让你的专属顾问为你服务

1.0169s