Let's say we have an enumerated type E
.
enum class E : underlying_type_of_E {
v1 = uE1,
v2 = uE2,
//...
vN = uEN
};
typedef typename std::underlying_type<E>::type uE;
In general, not all values of uE
are valid values of E
, because we can choose the relationship between them. Is there a general way of creating random, valid (named in definition, not assignable), values of E?
This, for example would not work:
std::mt19937 m;
std::uniform_int_distribution<uE> randomUE(0, std::numeric_limits<uE>::max());
E e = static_cast<E>( randomUE(m) );
because:
- Value range may not start from 0
- Value range may not end at std::numeric_limits::max()
- Value range may not be a range at all - we can select discrete values for E from uE, for example {1, 3, 64, 272}.
Given that all of the enumerated values are known at compile-time, I cannot imagine a reason why would this be in any way dangerous, or error-prone.
As for a context of why I want such a thing - I'm working on a genetic algorithm that uses templated gene storage. For now, I use enums as chromosomes and store them in std::vector<bool>
which is converted into std::vector<enumT>
on demand. The problem with this approach is mutation that flips random bits with given probability. That can cause problems, as it can produce invalid chromosomes that are unnamed enum values.