快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

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

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

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

Reason(原因)

  • Simple repetition is tedious and error-prone.

    简单的重复多余且易错。

  • When you use auto, the name of the declared entity is in a fixed position in the declaration, increasing readability.

    当你使用auto的时候,被定义实体的名称会出现在固定的位置,这样可以增加可读性。          

  • In a template function declaration the return type can be a member type.

    在模板函数定义中,返回值可以是成员类型。

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://6mz.cn/article/gisgdc.html

其他资讯