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++(中文版)
176
5
explicit Person(int idx)
//
同前
: name(nameFromIdx(idx)) {}
private:
std::string name;
};
由于
std::string
型别并没有只接受单个整型形参的构造函数,所有
int
或者类
int
型别(例如,
std::size_t
short
long
)的实参都会汇集到接受
int
型别的那个
构造函数重载版本的调用。类似地,所有
std::string
型别的实参(包括可以构造出
std::string
型别对象之物,例如字面量“
Ruth
”)都会被传递给接受
std::string
型别的那个构造函数。综上,不会有让调用者出乎意料的情况。不过也许你可能会说
一些人会传递
NULL
0
来表示空指针,从而意外调用接受
int
型别的那个重载版本。
但是,这些人应该参阅条款
8
,而且要一遍遍地读,直到他们一想到要使用
NULL
0
表示空指针,就会立刻退缩为止。
标签分派
无论是传递左值常量还是传值,都不支持完美转发。如果使用万能引用的动机就是为
了实施完美转发,那就只能采用万能引用,别无他途。然而,我们也不想舍弃重载。
那么,如果一不想放弃重载,二不想放弃万能引用,那又该如何避免依万能引用型别
进行重载呢?
其实解决之道并非那么难。重载函数在调用时的决议,会考察所有重载版本的形参,
以及调用端传入的实参,然后选择全局最佳匹配的函数,这需将所有的形参/实参组
合都考虑在内。一个万能引用形参通常会导致的后果是无论传入了什么都给出一个精
确匹配结果,不过,如果万能引用仅是形参列表 ...
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

C++程序设计:原理与实践(基础篇)(原书第2版)

C++程序设计:原理与实践(基础篇)(原书第2版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749