惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Project Zero
Project Zero
F
Fortinet All Blogs
Recent Announcements
Recent Announcements
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
T
Tailwind CSS Blog
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
S
Schneier on Security
N
News and Events Feed by Topic
N
News | PayPal Newsroom
H
Help Net Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
Attack and Defense Labs
Attack and Defense Labs
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
A
About on SuperTechFans
AWS News Blog
AWS News Blog
S
Secure Thoughts
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
MyScale Blog
MyScale Blog
Martin Fowler
Martin Fowler
Help Net Security
Help Net Security
人人都是产品经理
人人都是产品经理
Latest news
Latest news
C
Cyber Attacks, Cyber Crime and Cyber Security
大猫的无限游戏
大猫的无限游戏
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
H
Hacker News: Front Page
P
Proofpoint News Feed
N
News and Events Feed by Topic
H
Heimdal Security Blog
L
Lohrmann on Cybersecurity
有赞技术团队
有赞技术团队
L
LangChain Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog

浮生笔记

时机的重要性 回答dayu博客的几个问题 学会做选择 我理想的城市 如何选择一个适合的城市 马来西亚游记 回首2023,展望2024 从ChatGPT聊起 菲律宾旅游攻略笔记 深圳游记 香港游记 怎样赚钱----我理解的商业模式 用户界面与跨平台 使用telnet通过IMAP协议读取QQ邮箱 信息成本-----从ChatGPT到情报学 买断制到订阅制----兼谈消费频次 我眼中的CSDN 迁移博客内容到静态博客 Hugo使用Jane主题支持搜索实现 C语言和Lua的相互调用示例代码 从放逐之城看经济学 不同产业的分析 回首2021,展望2022 游戏中的经济学 Windows获取网络地址、子网掩码等 -fsanitize=address 参数作用 C++ 编译器支持标准判断 C++ 编译器支持标准判断 VS Code的golang开发配置 之 代码提示 二叉树遍历的非递归算法的实现 我的Chrome插件 golang 获取get参数 关于写代码的几个看法 golang编程之我见 Linux 网络编程之 Select 构建之法读书笔记 (1) 友情链接 二进制协议 vs 文本协议 从重构到重写 asio制作使用ssl通信的证书 gdb 7.11 Linux 获取网卡信息 《构建之法》读后感 由买冰箱想到的 2014年年终总结 聊聊我对写好程序的认识 编程技巧之表格驱动编程 经验搜索排名---google已经做过类似的了(我想多了) 有关编程语言的认识
Effective Morden C++ 读书笔记(3)
DennisThink · 2017-08-20 · via 浮生笔记

文章目录

Item5 使用auto而不是确定的类型

代码链接:github

1使用auto必须初始化,否则会报错,而使用具体类型不会

1
2
3
int x ;
auto y;
auto z = 0;

对于上面的3行代码,代码编译的情况是: 1.int x ;没有正确初始化,编译器没有报错。 2.auto y;没有正确初始化,编译器报错了。错误消息:error: declaration of ‘auto y’ has no initializer auto y; 3.auto z = 0;正确初始化,编译器没有报错。 以上的三条语句表达的同样的意思,但是从代码的健壮性上来说,最后一条语句的健壮性最好,因此推荐使用auto 来声明变量。

2 使用auto可以进行更短类型声明

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Widget{
public:
    explicit Widget(){};
    bool operator   (const Widget& rhs){
        return true;
    }
};

void autoLamda() {
    auto printFunc = [](const std::unique_ptr<Widget>& lhs,
                        const std::unique_ptr<Widget>& rhs)->bool {
                            return *lhs   * rhs;
                        };
    printFunc(std::make_unique<Widget>(),std::make_unique<Widget>());
}

std::function bool(const std::unique_ptr<Widget>& lhs,const std::unique_ptr <Widget>& rhs)> notAutoFunc = [](const std::unique_ptr<Widget>& lhs,
                        const std::unique_ptr<Widget>& rhs)->bool {
                            return *lhs   * rhs;
                        };

void notAutoLamda(){
    notAutoFunc(std::make_unique<Widget>(),std::make_unique<Widget>());
}

在上面的代码中,autoLamda 中的 printFuncnotAutoFunc 声明了同样的函数类型的变量,但是使用auto的表达可以更为简洁,因此提倡使用 auto

3 在std容器中使用auto。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
void AutoVector()
{
    std::vector<Widget> intVector;
    auto count = intVector.size();

    for(const auto& it:intVector){

    }
}

void NormalVector()
{
    std::vector<Widget> intVector;
    std::size_t count = intVector.size();

    for(std::vector<Widget>::iterator it = intVector.begin() ;
        it != intVector.end();
        ++it)
    {

    }
}

上面的两个函数较好的展示了使用auto和传统方式在对于std::vector进行操作时候的不同。可以看出,使用auto的代码更为简短,当std::vector内的变量类型发生变化的时候,auto的版本改动最少。

4 总结:

auto类型必须被初始化(依靠初始化进行类型的判断)。对于类型不匹配具有免疫力(例如我们经常会把 std::vector.size()的返回值声明为 int,实际类型是 std::size_t。可以更好的进行重构,需要输入的字符更少。

文章作者 DennisThink

上次更新 2022-08-28

许可协议 CC BY-NC-ND 4.0