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

推荐订阅源

S
Schneier on Security
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
TaoSecurity Blog
TaoSecurity Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
爱范儿
爱范儿
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
SecWiki News
SecWiki News
MyScale Blog
MyScale Blog
AI
AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 【当耐特】
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Project Zero
Project Zero
T
Tor Project blog
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
IT之家
IT之家
The Hacker News
The Hacker News
腾讯CDC
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
C
Cisco Blogs
博客园 - 聂微东
Webroot Blog
Webroot Blog
Forbes - Security
Forbes - Security
M
MIT News - Artificial intelligence
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans

浮生笔记

时机的重要性 回答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