定义概念时是否允许使用 OR (`||`)?
Is OR (`||`) allowed when defining concepts?
我看到很多 "composite" 概念的例子是用 &&
定义的,但是 none 中使用了 ||
。这让我想知道使用 ||
是否有意义。在我看来,使用 ||
将允许指定一个概念,该概念具有类似于重载的效果(即临时多态性),但具有一组封闭的可能性。
对于一组"primitive"个概念C1,...,CN,写
是否有效
C1 || ... || CN
作为一个"composite"概念?
Constraints
A constraint is a sequence of logical operations that specifies
requirements on template arguments. They can appear within
requires-expressions (see below) and directly as bodies of concepts
There are 9 types of constraints:
- conjunctions
- disjunctions
- predicate constraints
- expression constraints (only in a requires-expression)
- type constraints (only in a requires-expression)
- implicit conversion constraints (only in a requires-expression)
- argument deduction constraints (only in a requires-expression)
- exception constraints (only in a requires-expression)
- parametrized constraints (only in a requires-expression)
和
Disjunctions
Disjunction of constraints P and Q is specified as P || Q.
A disjunction of two constraints is satisfied if either constraint is satisfied. Disjunctions are evaluated left to right and short-circuited (if the left constraint is satisfied, template argument deduction into the right constraint is not attempted). User-defined overloads of operator|| are not allowed in constraint disjunctions.
您几乎可以在约束中使用任何常量表达式(包括 ||
)。唯一的限制是那些表达式必须 return bool
.
||
的一个用途是定义已知共享公共接口的类型集。例如,积分 (std::is_integral
) 可以定义为多个相同类型约束 (is_same<T, int>
) 的析取。我不建议这样做。您应该以附加方式定义概念。
我看到很多 "composite" 概念的例子是用 &&
定义的,但是 none 中使用了 ||
。这让我想知道使用 ||
是否有意义。在我看来,使用 ||
将允许指定一个概念,该概念具有类似于重载的效果(即临时多态性),但具有一组封闭的可能性。
对于一组"primitive"个概念C1,...,CN,写
是否有效 C1 || ... || CN
作为一个"composite"概念?
Constraints
A constraint is a sequence of logical operations that specifies requirements on template arguments. They can appear within requires-expressions (see below) and directly as bodies of concepts There are 9 types of constraints:
- conjunctions
- disjunctions
- predicate constraints
- expression constraints (only in a requires-expression)
- type constraints (only in a requires-expression)
- implicit conversion constraints (only in a requires-expression)
- argument deduction constraints (only in a requires-expression)
- exception constraints (only in a requires-expression)
- parametrized constraints (only in a requires-expression)
和
Disjunctions
Disjunction of constraints P and Q is specified as P || Q. A disjunction of two constraints is satisfied if either constraint is satisfied. Disjunctions are evaluated left to right and short-circuited (if the left constraint is satisfied, template argument deduction into the right constraint is not attempted). User-defined overloads of operator|| are not allowed in constraint disjunctions.
您几乎可以在约束中使用任何常量表达式(包括 ||
)。唯一的限制是那些表达式必须 return bool
.
||
的一个用途是定义已知共享公共接口的类型集。例如,积分 (std::is_integral
) 可以定义为多个相同类型约束 (is_same<T, int>
) 的析取。我不建议这样做。您应该以附加方式定义概念。