Skip to content

Instantly share code, notes, and snippets.

@roehrdor
Last active March 3, 2019 06:47
Show Gist options
  • Save roehrdor/b8b9dd8112cb3839a4f9a8b58b61033c to your computer and use it in GitHub Desktop.
Save roehrdor/b8b9dd8112cb3839a4f9a8b58b61033c to your computer and use it in GitHub Desktop.
// https://godbolt.org/z/pujcf7
#include <utility>
#include <tuple>
namespace rz
{
namespace adl
{
using std::begin;
using std::end;
template <class T> auto adlbegin(T &&t)
{
return begin(std::forward<T>(t));
}
template <class T> auto adlend(T &&t)
{
return end(std::forward<T>(t));
}
} // namespace adl
namespace details
{
template <class T> std::add_lvalue_reference_t<T> declval();
} // namespace details
template <class T,
class TI = decltype(adl::adlbegin(details::declval<T>()))
>
constexpr auto enumerate(T &&t)
{
struct iterator
{
size_t i;
TI iter;
bool operator != (const iterator &other) const
{
return iter != other.iter;
}
iterator& operator++ ()
{
++this->i;
++this->iter;
return *this;
}
auto operator* () const
{
return std::tie(this->i, *this->iter);
}
};
struct wrapper
{
T iterable;
auto begin()
{
return iterator{0, adl::adlbegin(iterable)};
}
auto end()
{
return iterator{0, adl::adlend(iterable)};
}
};
return wrapper{ std::forward<T>(t) };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment