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
?
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
?
int i = 1;
Test val = static_cast<Test>(i);
auto
in this case? Is there any performance improvements? –
Caldwell 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. –
Cecillececily Test e = static_cast<Test>(1);
Your code:
enum Test
{
A, B
};
int a = 1;
Solution:
Test castEnum = static_cast<Test>(a);
static_cast
would be a better cast here. –
Tiresome 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. –
Masticate enums
all native types will result in a static_cast
, but for casting to another type it may change to a reinterpret_cast
. It is good practice to use the same types of casts everywhere in your code, so you shouldn't suddenly use C-style casts for enums
. –
Masticate static_cast<float>
? What about static_cast<string>
? –
Kerosene Test castEnum = static_cast<Test>(a);
would actually result in castEnum == Test::B
? I believe if you want to cast to Test::A, the value to cast will have to be 0, so: enum Test { A, B }
int a = 0;
Test castEnum = static_cast<Test>(a);
would have the desired result. Unless the result of Test::A is besides the point of the OP... –
Orchitis 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 typeT
using astatic_cast
of the formstatic_cast<T>(e)
if the declarationT 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);
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. –
Jurkoic 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. –
Detinue 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
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
.
© 2022 - 2024 — McMap. All rights reserved.
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... – Orchitis