网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

其他资讯