I am trying to create a concept ElementIterable
which can determine the type is nested ranges or not. For example, the elements in std::vector<int>
is not iterable, but the elements (std::vector<int>
) in std::vector<std::vector<int>>
is iterable. The idea about using std::iterator_traits<T>
comes up in my mind and the experimental code is as following. However, this ElementIterable
concept doesn't work as the expected behavior. Is there any idea to fix this ElementIterable
concept?
template<typename T>
concept ElementIterable = requires(typename std::iterator_traits<T>::value_type x) // requires-expression
{
x.begin(); // must have `x.begin()`
x.end(); // and `x.end()`
};
The usage of this ElementIterable
is here.
template<typename T> requires ElementIterable<T>
void Foo(T input);
template<typename T> requires ElementIterable<T>
void Foo(T input)
{
std::cout << "Element iterable" << std::endl;
}
template<typename T>
void Foo(T input);
template<typename T>
void Foo(T input)
{
std::cout << "Element not iterable" << std::endl;
}
The usage of the function Foo
.
int number = 1;
std::vector<decltype(number)> vector1;
vector1.push_back(number);
Foo(vector1); // Element not iterable
std::vector<decltype(vector1)> vector2;
vector2.push_back(vector1);
Foo(vector2); // Element not iterable
// but expected behaviour is: Element iterable
All suggestions are welcome.