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.3 合法代码

一组模板实参是否满足了模板对其参数的要求?这个问题最终归结为某些表达式是否合法。

使用requires表达式,可以检查一组表达式是否合法。例如:

其中,requires requires不是拼写错误。第一个requires表示requirements子句的开始,而第二个requires是requires表达式的开始:

一个requires表达式就是一个谓词,当其中的语句是合法代码时为true,否则为false。

我将requires表达式视为泛型编程的汇编代码。类似普通的汇编代码,requires表达式非常灵活,而且没有强加新的编程规则。它以这样或那样的形式出现在最有趣的泛型代码的底层,就像汇编代码出现在最有趣的普通代码的底层那样。类似于汇编代码,requires表达式不应该被视为“普通代码”。如果你在代码中看到了requires requires,这很可能是非常底层的代码。

在advance()中,我故意以一种不优雅、不自然的方式使用requires requires。注意,我“忘记了”指出+=以及操作所需的返回类型。我已经警告过你!应优先选择命名概念,用名字指示其语义含义。

总结起来,应优先选择使用恰当命名的概念和明确指定的语义(参见7.2.4节),并在其定义中使用requires表达式。 ...

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