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++(中文版)
192
5
果在
typedef
的创建或者评估求值的过程中出现了引用的引用,引用折叠就会出手消
灭之。例如,假设我们有个类模板
Widget
,内嵌一个右值引用型别的
typedef
template<typename T>
class Widget {
public:
typedef T&& RvalueRefToT;
};
再假设我们以左值引用型别来实例化该
Widget
Widget<
int&
> w;
Widget
中以
int&
代入
T
的位置,则得到如下的
typedef
typedef
int& &&
RvalueRefToT;
引用折叠又将上述语句化简得到:
typedef
int&
RvalueRefToT;
这个结果显然表明,我们为
typedef
选择的名字也许有些名不符实:当以左值引用型
别实例化
Widget
时,
RvalueRefToT
其实成了个左值引用的
typedef
最后一种会发生引用折叠的语境在于
decltype
的运用中。如果在分析一个涉及
decltype
的型别过程中出现了引用的引用,则引用折叠亦会介入并消灭之(有关
decltype
的信息,详见条款
3
)。
要点速记
引用折叠会在四种语境中发生:模板实例化、
auto
型别生成、创建和运用
typedef
和别名声明,以及
decltype
当编译器在引用折叠的语境下生成引用的引用时,结果会变成单个引用。
如果原始的引用中有任一引用为左值引用,则结果为左值引用。否则,结
果为右值引用。
万能引用就是在型别推导的过程会区别左值和右值,以及会发生引用折叠 ...
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