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++(中文版)
64
3
的型别当然永远是
int
,所以在这次对
lockAndCall
调用时的具现里,形参
ptr
的型别
就被定为
int
。不幸的是,这意味着在
lockAndCall
内部的
func
在调用时传入的是一
int
,但这和
f1
期望的
std::shared_ptr<Widget>
并不兼容。传入
lockAndCall
0
本意是想代表一个空指针,结果实际传入时却是把它理解成一个
int
。企图将
int
f1
来充数
std::shared_ptr<Widget>
会导致型别错误。使用
0
来调用
lockAndCall
之所以会失败,是因为
int
被传给了一个要求
std::shared_ptr<Widget>
的函数。
对于采用
NULL
的调用,分析过程八九不离十。形参
ptr
的型别被推导为某种整型,然
后在
ptr
这个具备
int
或相似于
int
的型别的实参被传递给
f2
时,型别错误就发生了,
因为
f2
要求一个
std::unique_ptr<Widget>
相比之下,采用
nullptr
的调用则毫无问题。当
nullptr
传给
lockAndCall
时,
ptr
的型别被推导为
std::nullptr_t
。当
ptr
被传递给
f3
时,从
std::nullptr_t
Widget*
存在隐式型别转换,因为
std::nullptr_t
以隐式转换到所有指针型别。
模板型别推导会将
0
NULL
导成“错误”型别(即它们的真实型别,而非退而求
其次的表示空指针这个意义),这个事实构成了应该在表示空指针时使用
nullptr
0
NULL
的压倒性理由。而使用 ...
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