0

How do I index into an enum via "item index" instead of the "value indexing":

"Value indexing" (not useful to me with what I am doing):

eSerialBauds eBaud = static_cast<eSerialBauds>(1200); // yields eBaud1200

I want "item indexing": ( How to get the following? )

eSerialBauds eBaud = static_cast<eSerialBauds>(3); // to yield eBaud1200

// values (from WinBase.h)
#define CBR_110             110
#define CBR_300             300
#define CBR_600             600
#define CBR_1200            1200
#define CBR_2400            2400



enum class eSerialBauds
{
  eBaud110 = CBR_110,
  eBaud300 = CBR_300,
  eBaud600 = CBR_600,
  eBaud1200 = CBR_1200,
  eBaud2400 = CBR_2400,
}

Please note that I am given this enum class from another class. There are many. So I have to work with what is given to me.

I did write a work around method but it would be nice to have a more elegant way of getting the result.

jdl
  • 6,151
  • 19
  • 83
  • 132
  • nvm I read too fast. I retract my duplicate vote. This is not [Conversion from int to enum class type possible?](https://stackoverflow.com/questions/53148463/conversion-from-int-to-enum-class-type-possible) – Ryan Haining Apr 14 '21 at 22:35
  • Is my question clear enough for others? – jdl Apr 14 '21 at 22:37
  • 1
    I think so, I was just careless but left the comment so no one else votes it as a dup of that as well. – Ryan Haining Apr 14 '21 at 22:43
  • The target I used uses `enum` instead of `enum class`, but it's basically the same question. Just ping me if you think that's not an appropriate target. – cigien Apr 14 '21 at 22:55
  • The question was closed, but in case you want to keep the same syntax, you could do: https://replit.com/@Ranoiaetep/ExperiencedDeadTerabyte#main.cpp – Ranoiaetep Apr 14 '21 at 23:15
  • @cigien which of the answers in the already answered solves the OP's problem? – Surt Apr 14 '21 at 23:53
  • @Surt That's irrelevant. If the questions are the same, then the question is a duplicate. Since an answer to the OP's question would work just fine on the target question, that's where an answer should be added (assuming it doesn't already exist). – cigien Apr 14 '21 at 23:56
  • The enumerations are used else where by others, that class is written. I want to set the enumerations given a config file I have a way around this, but would rather a more direct way using the enum class. – jdl Apr 15 '21 at 00:39
  • @cigien the answers there are only for contagious zero based enumerations which can't be used here, as the values are discontinuous, 110, 300, 600 etc. – Surt Apr 15 '21 at 07:59
  • @Surt Yes, I understand. But an answer that works for dicontiguous enumerations can be posted on the target question, and that is where the answer should go. – cigien Apr 15 '21 at 11:52
  • @cigen unlikely. The hard part of this question is that they are discontinuous. It being an `enum class` may very well lead to a different solution as well. I voted to reopen. – Ryan Haining Apr 15 '21 at 22:52

2 Answers2

1

Just set up an array containing the enum values, like this:

static const eSerialBauds bauds_by_index [] = { eBaud110, eBaud300, eBaud600, eBaud1200, eBaud2400 };

And then you can do, for example:

eSerialBauds baud = bauds_by_index [3];
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
1

The easiest way would be to build an array of the enum values.

static constexpr auto sBaudIndex = std::array{eBaud100, eBaud200, eBaud600, eBaud1200, eBaud2400);

Then you index that array. It is fragile, but I know of no way to have the compiler enumerate the enum values for you.

You could skip the enum if you don't need it, and just use the WinBase values in your array

Jason
  • 1,612
  • 16
  • 23