Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
微调
281
写并编译这样的代码,你能指望的最好结果就是它在运行时崩溃。如果你不那么走运,
你和你的调试器可能就得亲密好一阵子了。
先把
push_back
emplace_back
和亲密什么的按下不表,单注意下面几个非常相似的
初始化语法如何产生了不同的结果:
std::regex r1 = nullptr;
//
错误!无法通过编译
std::regex r2(nullptr);
//
能编译
用标准中的官方术语来说,用于初始化
r1
(采用等号)的语法对应于所谓复制初始化。
相对地,用于初始化
r2
的语法(使用括号,尽量也可以使用花括号代替)会产生所谓
的直接初始化。复制初始化是不允许调用带有
explicit
明饰词的构造函数的,但直
接初始化就允许。是故,对
r1
实施初始化的那一行通不过编译,但对
r1
实施初始化
的那一行就可以通过编译。
但是,回过头来再说
push_back
emplace_back
,或更一般地对插入函数和置入函数
作对比。置入函数使用的是直接初始化,所以它们就能够调用带有
explicit
声明饰词
的构造函数。而插入函数使用的是复制初始化,它们就不能调用带有
explicit
明饰
词的构造函数。因此:
regexes.emplace_back(nullptr);
//
能编译,直接初始化允许使用
//
接受指针的、带有
explicit
声明饰词的
// std::regex
构造函数
regexes.push_back(nullptr);
// ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749