为什么我们需要需求? [英] Why do we require requires requires?

查看:82
本文介绍了为什么我们需要需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 20概念的一个角落是,在某些情况下,您必须编写 requires require 。例如,此示例来自 [expr.prim.req] / 3

One of the corners of C++20 concepts is that there are certain situations in which you have to write requires requires. For instance, this example from [expr.prim.req]/3:


requires-expression 也可以用于 requires-clause >([temp])作为对模板参数(例如以下参数)编写临时约束的一种方式:

A requires-expression can also be used in a requires-clause ([temp]) as a way of writing ad hoc constraints on template arguments such as the one below:

template<typename T>
  requires requires (T x) { x + x; }
    T add(T a, T b) { return a + b; }

第一个要求引入需求条款,第二个要求引入 requires-expression

The first requires introduces the requires-clause, and the second introduces the requires-expression.

需要第二个需要关键字的技术原因是什么?为什么我们不能只允许写:

What is the technical reason behind needing that second requires keyword? Why can't we just allow writing:

template<typename T>
  requires (T x) { x + x; }
    T add(T a, T b) { return a + b; }

(注意:请不要回答语法需要它)

推荐答案

这是因为语法需要它。

It is because the grammar requires it. It does.

A 要求约束不必使用需要表达式。它可以使用或多或少的任意布尔常量表达式。因此, requires(foo)必须是合法的 requires 约束。

A requires constraint does not have to use a requires expression. It can use any more-or-less arbitrary boolean constant expression. Therefore, requires (foo) must be a legitimate requires constraint.

A 要求 表达式(测试某些事物是否遵循某些约束的事物)是一个独特的构造;它只是由相同的关键字介绍的。 requires(foo f)将是有效的 requires 表达式的开头。

A requires expression (that thing that tests whether certain things follow certain constraints) is a distinct construct; it's just introduced by the same keyword. requires (foo f) would be the beginning of a valid requires expression.

您想要的是,如果在接受约束的地方使用 requires ,您应该能够在其中使用约束+表达式 要求子句。

What you want is that if you use requires in a place that accepts constraints, you should be able to make a "constraint+expression" out of the requires clause.

所以这是问题:如果放置要求(foo)放到适合需求约束的位置...解析器必须走多远才能意识到这是一个需求 constraint 而不是constraint + expression

So here's the question: if you put requires (foo) into a place that is appropriate for a requires constraint... how far does the parser have to go before it can realize that this is a requires constraint rather than a constraint+expression the way you want it to be?

考虑一下:

void bar() requires (foo)
{
  //stuff
}

如果 foo 是类型,则(foo)是require表达式的参数列表,并且 {} 中的所有内容都不是该函数的主体,而是该需要表达式的主体。否则, foo requires 子句中的表达式。

If foo is a type, then (foo) is a parameter list of a requires expression, and everything in the {} is not the body of the function but the body of that requires expression. Otherwise, foo is an expression in a requires clause.

好吧,您可以说编译器应该只弄清楚 foo 是什么。但是C ++ 真的不喜欢它,因为解析标记序列的基本操作要求编译器在理解标记之前先弄清楚这些标识符的含义。是的,C ++是上下文相关的,因此确实会发生这种情况。但是委员会更愿意避免这种情况。

Well, you could say that the compiler should just figure out what foo is first. But C++ really doesn't like it when the basic act of parsing a sequence of tokens requires that the compiler figure out what those identifiers mean before it can make sense of the tokens. Yes, C++ is context-sensitive, so this does happen. But the committee prefers to avoid it where possible.

是的,这是语法。

这篇关于为什么我们需要需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆