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++(中文版)
198
5
以成功。在上面的例子中,编译器从
{1,2,3}
出发生成一个临时的
std::vector<int>
型别对象,从而
f
的形参就有了一个
std::vector<int>
对象得以绑定。
而经由转发函数模板
fwd
来对
f
实施间接调用时,编译器就不再会比较
fwd
的调用处
传入的实参和
f
中所声明的形参了。取而代之的是,编译器会采用推导的手法来取得
传递给
fwd
参的型别结果,尔后它会比较推导型别结果和
f
声明的形参型别。完美
转发会在下面两个条件中的任何一个成立时失败:
编译器无法为一个或多个
fwd
的形参推导出型别结果
。在此情况下,代码无法编
译通过。
编译器为一个或多个
fwd
的形参推导出了“错误的”型别结果
。这里所谓“错误的”,
既可以指
fwd
根据型别推导结果的实例化无法通过编译,也可以指以
fwd
推导而
得的型别调用
f
与直接以传递给
fwd
的实参调用
f
行为不一致。这种分裂行为的
源泉之一,可能在于
f
乃是个重载函数的名字,然后,依据“不正确的”推导型别,
fwd
里调用到的
f
重载版本,就与直接调用
f
的版本有异。
在上述“
fwd({1,2,3})
”这句调用一例中,问题在于向未声明为
std::initializer_
list
型别的函数模板形参传递了大括号初始化物,因为这样的语境按规定,用标准委
员会的行话说,叫做
“非推导语境”。通俗地说,这个词的意思是,由于
fwd
的形参
未声明为
std::initializer_list
,编译器就会被禁止在
fwd
的调用过程中从表达式
{1,2,3}
出发来推导型别。而既然从 ...
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