Skip to content

Concepts

是在阅读cppreference Named requirements的时候,发现了concepts特性的。

zhihu Concept 对 C++ 有什么影响,它和 Rust 的泛型 Bound 和 Trait 有点类似?

cppreference Constraints and concepts

NOTE: 查看右侧的目录可以知道,原文的行文思路是:

首先介绍constraint和concept的概念,然后对concept进行介绍,然后对constraint进行介绍。

Named sets of such requirements are called concepts. Each concept is a predicate, evaluated at compile time, and becomes a part of the interface of a template where it is used as a constraint:

NOTE: 这段话表述了concept和constrain的关系:我们可以定义concept,然后将这个concept用作constraint。

The intent of concepts is to model semantic categories (Number, Range, RegularFunction) rather than syntactic restrictions (HasPlus, Array). According to ISO C++ core guideline T.20, "The ability to specify a meaningful semantics is a defining characteristic of a true concept, as opposed to a syntactic constraint."

NOTE: concept提供给programmer来表达semantic的

Concepts

Constraints

Conjunctions

Disjunctions

Atomic constraints

Constraint normalization

Requires clauses

Requires expressions

Simple requirements

Type requirements

Compound Requirements

Nested requirements

Partial ordering of constraints