简体   繁体   English

为什么需要在 requires 子句中的否定表达式周围使用括号?

[英]Why are parentheses needed around negated expressions in a requires-clause?

The following code is an usage example of the requires -clause:以下代码是requires子句的使用示例:

#include <type_traits>

template <typename T>
  requires std::is_integral_v<T>
void take_integral(T value);

It accepts an expression that evalutes to a bool value ( std::is_integral_v<T> in this case), and works as expected.它接受一个计算为bool值的表达式(在本例中为std::is_integral_v<T> ),并按预期工作。 However, when such expression is negated with the !然而,当这样的表达式被! operator, it results in a compilation error:运算符,它会导致编译错误:

#include <type_traits>

template <typename T>
  requires !std::is_integral_v<T>
void take_integral(T value);

Diagnostic from GCC:来自 GCC 的诊断:

<source>:4:12: error: expression must be enclosed in parentheses
    4 |   requires !std::is_integral_v<T>
      |            ^~~~~~~~~~~~~~~~~~~~~~
      |            (                     )
Compiler returned: 1

Diagnostic from Clang:来自 Clang 的诊断:

<source>:4:12: error: parentheses are required around this expression in a requires clause
  requires !std::is_integral_v<T>
           ^~~~~~~~~~~~~~~~~~~~~~
           (                     )
1 error generated.
Compiler returned: 1

Why are parentheses needed here?为什么这里需要括号?

Parentheses are required because they avoid language parsing ambiguities.括号是必需的,因为它们可以避免语言解析歧义。

Not every expression is allowed inside a requires -clause.并非所有表达式都允许在requires子句中。 In fact, the standard gives an example of how a parsing ambiguity would arise if all expressions were allowed:事实上,该标准给出了一个示例,说明如果允许所有表达式,将如何产生解析歧义:

[temp.pre]/9 [temp.pre]/9

[...] The expression in a requires-clause uses a restricted grammar to avoid ambiguities. [...] requires 子句中的表达式使用受限制的语法来避免歧义。 Parentheses can be used to specify arbitrary expressions in a requires-clause .括号可用于在requires 子句中指定任意表达式。 [ Example: [ 例子:

 template<int N> requires N == sizeof new unsigned short int f(); // error: parentheses required around == expression

— end example ] — 结束示例 ]

In the above example given by the standard, a compiler couldn't possibly know whether the sizeof part should be parsed as a sizeof of the expression new unsigned short or new unsigned short int .在标准给出的上述示例中,编译器不可能知道是否应该将sizeof部分解析为表达式new unsigned shortnew unsigned short intsizeof Putting parentheses around it, like requires (N == sizeof new unsigned short) , solves the problem.在它周围加上括号,比如requires (N == sizeof new unsigned short) ,解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么类外成员模板定义需要重复其声明“requires-clause” - Why does an out-of-class member template definition need a repetition of its declaration 'requires-clause' 为什么可变参数模板在模板引入中不起作用,而在需求子句中起作用? ConceptName {T,U,V,W} &lt;—模板 <typename …T> - Why does variadic template not work in template introduction but work in requires-clause? ConceptName{T,U,V,W} <— template<typename …T> 模板函数要求子句的原子约束中的替换失败 - Substitution failure in an atomic constraint of template function requires-clause Requires-clause 出现在模板模板参数之后:这是合法的语法吗? - Requires-clause appears after template template parameter: is this legal grammar? || 的无关可见过载防止|| 从要求子句中的短路 - Unrelated visible overload of || prevents || from short-circuiting in a requires-clause 围绕返回值的括号 - 为什么? - Parentheses around return values - why? 围绕表达式的括号和括号基本上都做同样的事情吗? - Do both parentheses and braces around expressions basically do the same thing? 当 `require 子句` 被括号括起来时,为什么用括号括起来模板参数? - Why surround template parameters with parentheses when `require clause` is surrounded by parentheses? 在static_strlen的以下实现中,为什么围绕str的&和括号是必要的? - In the following implementation of static_strlen, why are the & and parentheses around str necessary? 为什么用括号括起来的令牌不是r值表达式? - Why are tokens wrapped by parentheses not r-value expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM