300

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 Test::A?

2
  • 1
    link Note that it doesn't matter whether the int matches one of the constants of the enum type; the type conversion is always illegal.
    – Labokas
    Mar 29, 2014 at 11:39
  • 8
    I believe that if you want to cast to Test::A the value of int a will have to be 0, because Test::A has an implicit value of 0 and Test::B has an implicit value of 1. Unless the fact of casting specifically to Test::A is besides the point... Jan 24, 2016 at 14:10

6 Answers 6

346
int i = 1;
Test val = static_cast<Test>(i);
6
  • 34
    auto val = static_cast<Test>(i); // C++11
    – Mitch
    Sep 30, 2016 at 17:58
  • 7
    @Mitch what do I get for using auto in this case? Is there any performance improvements? May 19, 2017 at 3:29
  • 9
    No performance improvements. Compiler just deduces the type automatically if you specify with "auto". If you decide to change your enum name in the future, you will be modifying your code less since compiler will automatically deduce the correct type name.
    – avernus
    Dec 21, 2019 at 14:35
  • 14
    @AydinÖzcan Modern IDEs can easily rename anything throughout your whole codebase.
    – Hugius
    Sep 16, 2020 at 15:07
  • 3
    I would say the bigger improvement than ease of refactoring is mainly for things with long type signatures: auto myptr = std::make_shared<my::cool::type::class>(1, 2, 3, 4, 5); is much shorter than specifying the full type of myptr, and the right-hand side of the assignment makes it clear what the type is anyway. Apr 5, 2021 at 13:26
86
Test e = static_cast<Test>(1);
8
  • 18
    MSDN: The static_cast operator can explicitly convert an integral value to an enumeration type. If the value of the integral type does not fall within the range of enumeration values, the resulting enumeration value is undefined. Jul 12, 2012 at 13:56
  • 2
    @KirillKobelev if the integral value can be represented by the underlying type of the enum then the resulting enum must have that value. Otherwise the produced enum value will be whatever value results from converting the expression to the enum's underlying type. If VC++ does something different then I think it's non-conformant.
    – bames53
    Jul 12, 2012 at 17:09
  • 4
    what a conformant compiler should do, if enum has values { 1,3,5 } and code attempts to do <static_cast> from the value of 2. How will that differ from the C-cast? Jul 13, 2012 at 0:23
  • 6
    @KirillKobelev I'm not using a static_cast because it does anything different from a C style cast, I'm using static_cast because C++ casts are stylistically preferable to C casts.
    – bames53
    Jul 13, 2012 at 2:18
  • 4
    @KirillKobelev "if enum has values { 1,3,5 }" No. The enumeration type cannot be limited to only these 3 possible values: { 1,3,5 } are the enumerators (named enumeration values), not the enumeration itself. If 1,3,5 are possible enumeration values, then so is 2.
    – curiousguy
    Jul 15, 2012 at 7:34
32

Your code:

enum Test
{
    A, B
};

int a = 1;

Solution:

Test castEnum = static_cast<Test>(a);
9
  • 51
    It's a good idea to use the most restrictive cast you can, and avoid C-style casts altogether, to give the compiler it's best chance at detecting mistakes. static_cast would be a better cast here. Jul 12, 2012 at 13:57
  • 4
    @Mike Seymour, the problem is that static cast has no difference from the C-cast in this case. How and what mistake it can detect??? Jul 13, 2012 at 0:19
  • 7
    @KirillKobelev: The problem is that a C-style cast is not explicit. It can be equal to a static_cast, but it could as well be a const_cast or even worse, a reinterpret_cast or even a combination of those. Even if you know now in what it will degrade, suppose you change a to another type later on, it could very well be the type of casting changes without you ever getting as much as a warning, you don't want that.
    – KillianDS
    Jul 13, 2012 at 7:01
  • 5
    @KillianDS "suppose you change a to another type later on" which type?
    – curiousguy
    Jul 15, 2012 at 7:14
  • 2
    Yes, either those or an implicit cast if available. It is much clearer to what the intent of the cast is.
    – KillianDS
    Jul 16, 2012 at 5:28
27

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 only because this seems to be a popular question and nobody else has mentioned the alternative, per the C++11 standard:

5.2.9 Static cast

... an expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (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);
3
  • this solution worked in case compiler option blocked static_cast<> (semantic check). Not that it makes sense to me, but still neat.
    – Mr Buisson
    Nov 27, 2019 at 12:29
  • The solution provided here works for me, but I'm also curious as to why Test result(a); does NOT work, when it seems equivalent. It results in an error "Cannot initialize a variable of type 'Test' with an lvalue of type 'int'", where this seems to be exactly what the provided solution does too. Mar 21, 2021 at 18:28
  • 4
    @BillHollings Test result(a); looks like a constructor call for type Test with variable result, providing an arugment a. Because Test is just enumerated type, not a class or struct, you can't call it like constructor. But the Test(a) is a type conversion, so they are not equivalent — (Test)a also works.
    – rosshjb
    May 4, 2021 at 12:02
4

Just to mention it, if the underlying type of the enum happens to be fixed, from C++17 on, it is possible to simply write

enum Test : int {A, B};
int a = 1;
Test val{a};

and, of course, Test val{1}; is also valid.

The relevant cppreference part reads (emphasis mine):

An enumeration can be initialized from an integer without a cast, using list initialization, if all of the following are true:

  • the initialization is direct-list-initialization
  • the initializer list has only a single element
  • the enumeration is either scoped or unscoped with underlying type fixed
  • the conversion is non-narrowing
1

Test castEnum = static_cast<Test>(a-1); will cast a to A. If you don't want to substruct 1, you can redefine the enum:

enum Test
{
    A:1, B
};

In this case Test castEnum = static_cast<Test>(a); could be used to cast a to A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.