cppreference.com中指出:
概念(concept)不能递归地引用自身
但是我们如何定义一个概念,代表整数、整数向量,或整数向量的向量等。
我可以有如下内容:
template < typename Type > concept bool IInt0 = std::is_integral_v<Type>;
template < typename Type > concept bool IInt1 = IInt0<Type> || requires(Type tt) { {*std::begin(tt)} -> IInt0; };
template < typename Type > concept bool IInt2 = IInt1<Type> || requires(Type tt) { {*std::begin(tt)} -> IInt1; };
static_assert(IInt2<int>);
static_assert(IInt2<std::vector<int>>);
static_assert(IInt2<std::vector<std::vector<int>>>);
但我希望有类似于 IIntX
的东西,可以表示任何N的IIntN。
这种可能吗?
if constexpr
或等效代码,这最终会使整个概念失去意义?确实,你应该非常仔细地考虑在概念中使用||
的情况。 - Nicol Bolas||
来说,支持无限嵌套的容器是很困难的。 - Yakk - Adam Nevraumontint
和vector<vector<vector<int>>>
都有意义的执行某些操作? - Barryvalue_type
,直到找到一个非范围的东西,并且所有各种范围都必须同意相同的value_type
。但是没有办法在概念中轻松表达这一点。或者至少,不用去做可怕的、非概念的事情。 - Nicol Bolas