【发布时间】:2021-01-21 09:22:15
【问题描述】:
在 C++ 中,我想将 Foo<T> 类的模板参数 T 严格限制为 Foo<T> 的继承者。为此,我写了以下内容:
template <typename TSelf> class Foo;
template <typename TSelf>
requires (is_base_of<Foo<TSelf>, TSelf>)
class Foo
{
};
我得到的错误是
'Foo': requires clause is incompatible with the declaration
我该如何解决这个问题?
【问题讨论】:
-
你不能。即使此错误已解决,这也不是首发。检查可派生性需要完全定义的对象类型。因此
class A : Foo<A> ...总是格式错误的。 -
@Dmitri Nesteruk 在不相关的说明中,感谢您在 PluralSight 上制作这些视频 :)
-
@StoryTeller-UnslanderMonica 谢谢,有什么解决方法吗?我猜
static_assert也不起作用? -
@M.A 非常感谢!
标签: c++ templates constraints c++20 c++-concepts