How do I cast an int to an enum in C++?
For example:
enum Test
{
A, B
};
int a = 1;
How do I convert a to type
Spinning off the closing question, "how do I convert a to type Test::A" rather than being rigid about the requirement to have a cast in there, and answering several years late just this seems to be a popular question nobody else seems to have mentioned the alternative, per the C++11 standard:
5.2.9 Static cast
... an expression
ecan be explicitly converted to a typeTusing astatic_castof the formstatic_castif the declaration(e) T t(e);is well-formed, for some invented temporary variablet(8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion.
Therefore directly using the form t(e) will also work, and you might prefer it for neatness:
auto result = Test(a);