如何定义递归概念
How to define a recursive concept?
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>>>);
但是我想拥有类似 IIntX
的东西,这意味着任何n。
可能吗?
概念始终可以推迟到类型特征:
template <typename T> concept C = some_trait<T>::value;
,该性状可以是递归的:
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> { };
如果您不只是vector
,则可以将最后一个部分专业化推广到:
template <std::Range R>
struct some_trait<R> : some_trait<std::range_value_t<R>> { };
相关文章:
- 此递归模板类型定义是否有效C++?
- 为什么当函数参数未定义为常量引用时存在无限递归?
- 通过 lambda 定义的 constexpr 递归函数
- 这是定义一组递归规则的正确方法吗?
- 具有全局定义变量的递归比没有全局定义变量的递归函数获得更多的堆栈.为什么?(跳入C++)
- 精神:不能在其规则定义中使用x3::skip(skipper)[一些递归规则]
- 共享指针中的递归,而变量在类中定义
- 如何定义递归概念
- 递归定义数组中的数据对齐和排序
- 递归std ::函数定义模板类方法
- 无限循环与无限递归.两者都未定义吗?
- 如何从非递归版本定义斐波那契函数
- C 递归类型定义
- 递归模板类别定义的基本案例要求
- 模拟递归定义的类似数组类的聚合初始化
- 递归中的锚点大小写定义
- 自然数(递归)的设定理论定义
- 如何用C++递归地从二进制文件中读取自定义字符串
- 使用带有使用constexpr递归定义的类型的C++11 initializer_list
- 从递归定义的类继承