template<typename E, E v>
struct EnumValue
{
static const E value = v;
};
template<typename h, typename t>
struct StaticList
{
typedef h head;
typedef t tail;
};
template<typename list, typename first>
struct CyclicHead
{
typedef typename list::head item;
};
template<typename first>
struct CyclicHead<void,first>
{
typedef first item;
};
template<typename E, typename list, typename first = typename list::head>
struct Advance
{
typedef typename list::head lh;
typedef typename list::tail lt;
typedef typename CyclicHead<lt, first>::item next;
static void advance(E& value)
{
if(value == lh::value)
value = next::value;
else
Advance<E, typename list::tail, first>::advance(value);
}
};
template<typename E, typename f>
struct Advance<E,void,f>
{
static void advance(E& value)
{
}
};
/// Test enum
enum class Fruit
{
apple,
banana,
orange,
pineapple,
lemon
};
/// Scalable way, C++03-ish
typedef StaticList<EnumValue<Fruit,Fruit::apple>,
StaticList<EnumValue<Fruit,Fruit::banana>,
StaticList<EnumValue<Fruit,Fruit::orange>,
StaticList<EnumValue<Fruit,Fruit::pineapple>,
StaticList<EnumValue<Fruit,Fruit::lemon>,
void
> > > > > Fruit_values;
Fruit& operator++(Fruit& f)
{
Advance<Fruit, Fruit_values>::advance(f);
return f;
}
#include <iostream>
std::ostream& operator<<(std::ostream& os, Fruit f)
{
switch(f)
{
case Fruit::apple: os << "Fruit::apple"; return os;
case Fruit::banana: os << "Fruit::banana"; return os;
case Fruit::orange: os << "Fruit::orange"; return os;
case Fruit::pineapple: os << "Fruit::pineapple"; return os;
case Fruit::lemon: os << "Fruit::lemon"; return os;
}
}
int main()
{
Fruit f = Fruit::banana;
std::cout << "f = " << f << ";\n";
std::cout << "++f = " << ++f << ";\n";
}
ICAgIHRlbXBsYXRlPHR5cGVuYW1lIEUsIEUgdj4KICAgIHN0cnVjdCBFbnVtVmFsdWUKICAgIHsKICAgICAgICBzdGF0aWMgY29uc3QgRSB2YWx1ZSA9IHY7CiAgICB9OwogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBoLCB0eXBlbmFtZSB0PgogICAgc3RydWN0IFN0YXRpY0xpc3QKICAgIHsKICAgICAg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