问题描述
与下面的无效代码等效的道德是什么?
What is the moral equivalent to the following invalid code?
// Suppose you want to check, as part of a concept,
// if some operation on a type results in a type that models such concept.
// you cannot constrain the resulting type with the same concept
// in the same way you can't have self referential types
// (that would result in recursive definitions)
template<class T>
concept Int = requires(const T& a, const T& b) {
{ a + b } -> Int; // compiler don't know what to do
};
推荐答案
那是无限的递归.像任何函数递归一样,您必须具有终止条件.定义模板参数终止条件的通常方法是通过专门化.但是 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 concept
s 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.
这篇关于自引用C ++ 20概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!