Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a recursive concept?

The cppreference.com states that:

Concepts cannot recursively refer to themselves

But how can we define a concept that will represent an integer or a vector of integers, or a vector of vector of integers, etc.

I can have something this:

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>>>);

But I want to have something like IIntX that will mean IIntN for any N.

Is it possible?

like image 463
Vahagn Avatar asked Jun 24 '19 17:06

Vahagn


People also ask

What is a recursive definition in philosophy?

Recursion (adjective: recursive) occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic.

What is recursion and how to use recursion in programming?

What is recursion and how to use recursion in programming. If you are new to programming, then recursion concept is tough to understand. In this post, i’ll explain the concept of recursion with example. What is Recursion ? In Recursion , function call itself repeatedly, until the base or terminating condition is not true.

What is recursive algorithm?

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

What are the parts of a recursive function?

The Recursive Function has 2 parts: 1 The value of the smallest or the first term in the sequence, usually given as f (0) or f (1) 2 The pattern or the rule which can be used to get the value of any term, given the value of the term preceding it. In... More ...

What is an example of a recursive problem?

Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. What is base condition in recursion? In the recursive program, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems.


Video Answer


1 Answers

Concepts can always defer to a type trait:

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

And that trait can be recursive:

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> { };

If you don't mean just vector, then the last partial specialization can be generalized to:

template <std::Range R>
struct some_trait<R> : some_trait<std::range_value_t<R>> { };
like image 159
Barry Avatar answered Oct 12 '22 02:10

Barry