Like Matthieu said it is perfectly safe to do so and does not go against the C++ standard in any way.
As a side note overloading ++ operator over enum is not only an overkill, but has issues already stated by others (e.g. if enum is not linear in values)
Therefore instead of defining an operator ++, what you need is an iterator.
I adapted the following solution from here regarding enums by deft_code but slightly tailored to your example.
template< typename T >
class Enum
{
public:
class Iterator
{
public:
Iterator( int value ) :
m_value( value )
{ }
T operator*( void ) const
{
return (T)m_value;
}
void operator++( void )
{
++m_value;
}
bool operator!=( Iterator rhs )
{
return m_value != rhs.m_value;
}
private:
int m_value;
};
};
template< typename T >
typename Enum<T>::Iterator begin( Enum<T> )
{
return typename Enum<T>::Iterator( (int)T::First );
}
template< typename T >
typename Enum<T>::Iterator end( Enum<T> )
{
return typename Enum<T>::Iterator( ((int)T::Last) + 1 );
}
enum class MyEnum
{
FOO,
BAR,
BLECH,
NUM_ENUMS
};
int main()
{
for( auto e: Enum<MyEnum>() )
{
std::cout << ((int)e) << std::endl;
}
}
This may still be an overkill for you, but its much cleaner. Another piece of code but much more elegant approach is here. It is possible in the future enum classes introduced in C++11 may have an iterator in the standard.
update: Since you updated us that your values are contiguous and the fact that it is required for a C API (?) then the above will not be suitable