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++(中文版)
型别推导
23
template<typename T>
void f1(T param);
//
f1
中,
param
按值传递
template<typename T>
void f2(T& param);
//
f1
中,
param
按引用传递
f1(someFunc);
// param
被推导为函数指针,
//
具体型别是
void (*)(int, double)
f2(someFunc);
// param
被推导为函数引用,
//
具体型别是
void (&)(int, double)
在实践中,这些型别推导结果和前面讲过的没什么不一样。不过,如果你打算了解数
组向指针的退化,那么就顺便也了解一下函数向针对的退化好了。
关于和
auto
相关的模板型别推导规则,内容就这么多了。在本条款一开始,我就强
调过这些规则相当单刀直入,而事实上在大部分情况下它们也确实如此。特殊情况仅
仅在于,在对万能引用进行推导时传入左值的情况下,会有一点小小的波澜。再有,
就是数组和函数向指针退化的规则会将这小小的波澜搅成更大的漩涡。有时,你会想
直接揪住编译器并命令道:“告诉我,你推导出来的究竟是什么型别!”如果真是如此,
请翻到条款
4
,因为它专门讨论如何忽悠编译器去为你做这件事。
要点速记
在模板型别推导过程中,具有引用型别的实参会被当成非引用型别来处理。
换言之,其引用性会被忽略。
对万能引用形参进行推导时,左值实参会进行特殊处理。
对按值传递的形参进行推导时, ...
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
Effective STL

Effective STL

Scott Meyers

Publisher Resources

ISBN: 9787519817749