如何定义一个递归概念?

Vah*_*agn 9 c++ recursion c++-concepts c++20

cppreference.com指出:

概念不能递归引用自己

但是我们如何定义一个表示整数或整数向量或整数向量的概念,等等。

我可以有这样的东西:

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>>>);
Run Code Online (Sandbox Code Playgroud)

但我想有像IIntX这将意味着IINT ň任何N.

可能吗?

Bar*_*rry 12

概念始终可以遵循类型特征:

template <typename T> concept C = some_trait<T>::value;
Run Code Online (Sandbox Code Playgroud)

该特征可以是递归的:

template <typename T>
struct some_trait : std::false_type { };

template <std::Integral T>
struct some_trait<T> : std::true_type { };

template <typename T, typename A>
struct some_trait<std::vector<T, A>> : some_trait<T> { };
Run Code Online (Sandbox Code Playgroud)

如果您不是公正的vector,那么可以将最后的部分专业化概括为:

template <std::Range R>
struct some_trait<R> : some_trait<std::range_value_t<R>> { };
Run Code Online (Sandbox Code Playgroud)