为什么概念提炼不能使用简洁的语法 [英] Why can't concept refinement use the terse syntax

查看:49
本文介绍了为什么概念提炼不能使用简洁的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在精炼概念时,在标准中始终如一地完成的方式是完全写出要精炼的概念。例如,在 [concepts.integral] 中, SignedIntegral 像这样精炼 Integral

When refining concepts, the way it is consistently done in the standard is to fully write out the concept being refined. For instance, in [concepts.integral], SignedIntegral refines Integral like so:

template<class T>
  concept Integral = is_integral_v<T>;
template<class T>
  concept SignedIntegral = Integral<T> && is_signed_v<T>;

为什么精炼概念不能写成:

Why can't the refined concept be written as:

template<Integral T>
  concept SignedIntegral2 = is_signed_v<T>;

SignedIntegral2 似乎具有相同的明显含义 SignedIntegral 的对象,但它甚至不能在clang上编译。

SignedIntegral2 seems to have the same obvious meaning of SignedIntegral, but it doesn't even compile on clang. Is there a reason for this?

推荐答案

SignedIntegral2 的声明是由于 [temp.concept] / 4


一个概念不应具有关联的约束。

A concept shall not have associated constraints.

重要的是要了解其原因。概念基本上是谓词。他们的工作是接受一系列参数(最常见的是一系列类型),并说出是否满足该概念。但是请考虑这两种不同的实现将给出什么答案:

And it's important to understand the reason for this. Concepts are basically predicates. Their job is to take a series of arguments (most commonly, a series of types) and say whether the concept is satisfied or not. But consider what answer these two different implementations would give:


  • SignedIntegral< int32_t> true

  • SignedIntegral< uint32_t> false

  • SignedIntegral< string> false

  • SignedIntegral<int32_t> is true
  • SignedIntegral<uint32_t> is false
  • SignedIntegral<string> is false

但是:


  • SignedIntegral2< int32_t> true

  • SignedIntegral2< uint32_t> ; false

  • SignedIntegral2< string> 是...未定义

  • SignedIntegral2<int32_t> is true
  • SignedIntegral2<uint32_t> is false
  • SignedIntegral2<string> is... undefined

概念的整个重点是约束。 SignedIntegral2 类型参数 T 的拟议替代性简洁声明声明为积分。由于 string 不满足 Integral 的要求,我们甚至不能问它是否是 SignedIntegral2的问题。

The whole point of concepts is to constrain. The proposed alternative, terse declaration in SignedIntegral2 constrains the type parameter T to be Integral. Since string does not satisfy Integral, we cannot even ask the question of if it's a SignedIntegral2.

以另一种方式输入, SignedIntegral 是一个总函数,但 SignedIntegral2 是仅在 Integral 类型上定义的部分函数。如果我们将两者都写成实际上是函数,则可能会更清楚:

Put a different way, SignedIntegral is a total function but SignedIntegral2 is a partial function that is only defined on Integral types. This might be more clear if we write both to actually be functions:

template <typename T>
constexpr bool SignedIntegral() { return Integral<T> && is_signed_v<T>; }

template <Integral T>
constexpr bool SignedIntegral2() { return is_signed_v<T>; }

重要的一点是,概念始终是整体功能,这就是不允许关联约束的原因。

It is important that concepts always be total functions, which is why associated constraints are disallowed.

请注意,作为扩展,可以将未定义视为 false

Note that it is surely possible as an extension to treat "undefined" as false for the purposes of concepts satisfaction, but this would add extra wrinkles to the subsumption rules and it's surely non-trivial implementation complexity. It's certainly possible that some future standard might allow them. My crystal ball is currently at the shop, so I can't say for sure.

这篇关于为什么概念提炼不能使用简洁的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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