Is there any way to check if an arbitrary variable type is iterable?
So to check if it has indexed elements or I can actually loop over it\'s children? (Use foreach
Or if (like me) you hate every SFINAE solution being a big block of dummy struct definitions with ::type and ::value nonsense to wade through, here's an example of using a quick and (very) dirty one-liner:
template <
class Container,
typename ValueType = decltype(*std::begin(std::declval()))>
static void foo(Container& container)
{
for (ValueType& item : container)
{
...
}
}
The last template argument does multiple things in one step:
begin() member function, or equivalent.begin() function returns something that has operator*() defined (typical for iterators).Limitation: Doesn't double-check that there's a matching end() member function.
If you want something more robust/thorough/reusable, then go with one of the other excellent proposed solutions instead.