Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何定义递归概念?_C++_Recursion_C++20_C++ Concepts - Fatal编程技术网

C++ 如何定义递归概念?

C++ 如何定义递归概念?,c++,recursion,c++20,c++-concepts,C++,Recursion,C++20,C++ Concepts,缔约国指出: 概念不能递归地引用它们自己 但是,我们如何定义一个表示整数或整数向量,或整数向量向量等的概念呢 我可以吃这样的东西: template < typename Type > concept bool IInt0 = std::is_integral_v<Type>; template < typename Type > concept bool IInt1 = IInt0<Type> || requires(Type tt) { {*s

缔约国指出:

概念不能递归地引用它们自己

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

我可以吃这样的东西:

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>>>);
templateconcept bool IInt0=std::is\u integral\u v;
模板concept bool IInt1=IInt0 | |需要(类型tt){{{*std::begin(tt)}->IInt0;};
模板concept bool IInt2=IInt1 | |需要(类型tt){{{*std::begin(tt)}->IInt1;};
静态断言(IInt2);
静态断言(IInt2);
静态断言(IInt2);
但是我想有一些类似于
IIntX
的东西,这将意味着任何N的IIntN


有可能吗?

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

template <typename T> concept C = some_trait<T>::value;

我很好奇你到底想把这个概念应用到什么代码上。如何编写能够以相同方式操作整数和向量的代码,而不使用一堆
if constexpr
或等效代码,这最终会使整个概念变得毫无意义?事实上,您应该非常仔细地研究
|
在concept@NicolBolas
|
很难支持无限嵌套容器。@Yakk AdamNevraumont我认为指针更。。。你的算法对
int
vector
都有意义吗?@Barry这并不难?我的意思是,“递增每一个int”对两者都有意义。自动解除单子绑定(或对其内容进行操作)并不疯狂,即使它需要任意下降。在我看来,自动拳击是疯狂的;另一种方法,特别是在单个操作上,是可以的。@Yakk AdamNevraumont:也许更合理的用例是一个函数的概念,它接受一个范围并将范围展平。从逻辑上讲,这样的东西应该能够任意向下挖掘每个内部范围的
值\u类型
,直到找到一个非范围的东西,所有不同的范围必须在相同的
值\u类型
上达成一致。但是没有办法用一个概念轻松地表达这一点。或者至少,不是没有去恐怖的,非概念性的东西。
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> { };
template <std::Range R>
struct some_trait<R> : some_trait<std::range_value_t<R>> { };