且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自引用C ++ 20概念

更新时间:2023-02-03 07:45:16

假设作为概念的一部分,您要检查对某个类型的某些操作是否导致对该概念进行建模的类型.

Suppose you want to check, as part of a concept, if some operation on a type results in a type that models such concept.

那是无限的递归.像任何函数递归一样,您必须具有终止条件.定义模板参数终止条件的通常方法是通过专门化.但是 concept 的显式 不能专门化,因此不会有终止条件.

That's infinite recursion. Like any functional recursion, you have to have a terminal condition. The normal way to define a terminal condition for template arguments is via a specialization. But concepts explicitly cannot be specialized, so there can be no terminal condition.

从逻辑上讲,它也是不连贯的,因为您试图使用要定义的东西来编写定义.定义上没有意义的东西没有道德等价物".

It's also logically incoherent, since you're trying to write a definition by using the thing you're trying to define. There is no "moral equivalent" to something that by definition doesn't make sense.

您的概念似乎是在说:" T 应该是我可以添加到另一个 T 并产生...的东西",这是什么?您是否希望它能够产生一些不相关的类型 U ,可以将其添加到另一个 U 中以再次产生...,那又是什么呢?即使忽略了这个问题, U 是否应该能够添加到 T 中?如果是这样,那应该产生什么?

Your concept appears to be saying "T shall be a thing that I can add to another T and yield..." what? Do you want it to be able to yield some unrelated type U which can be added to another U to yield... again, what? Even ignoring that question, should U be able to be added to T? And if so, what should that yield?

在编写概念时,先从用例开始,首先确定要执行的操作.

When writing a concept, start with the use case, start by deciding what operations you want to perform.