Skip to Content
C++语言导学(原书第2版)
book

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

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

7.2.2 基于概念的重载

一旦已经恰当地用接口说明了模板,那么可以基于其属性来进行重载,这和函数几乎一样。考虑标准库函数advance()(参见12.3节)的一个略微简化的版本,它向前推进迭代器:

编译器会选择具有实参能满足的最强要求的模板。在本例中,list只支持前向迭代器,而vector则提供随机访问迭代器,于是有:

类似其他重载,这是一种编译时机制,意味着不会有运行时额外开销,而且当编译器无法找到一个最佳选择时,它会给出一个歧义错误。基于概念的重载规则远比通用重载的规则(参见1.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

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280