get a digit of an enum

hello forum
I have a enum data type which is

1
2
3
4
5
6
7
8
enum Type {

	D1Q3,
	D2Q9,
	D3Q19,
	D3Q27

};


The first number is the dimension of my problem and the second number is another factor which I need to get for future calculation. I don't know how the get any arbitrary digit out of an enum datatype. I tried to convert enum to string and then get the second digit of string using stringify. by the way I want to get any digit of enum without converting to string. can anyone help me with this.
thanks in advance.
Last edited on
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int get_dimension(Type t)
{
	switch (t)
	{
	case D1Q3:
		return 1;
	case D2Q9:
		return 2;
	case D3Q19:
	case D3Q27:
		return 3;
	}
	return -1;
}
Alternatively you could encode these two numbers into the underlying value of each enumerator so that you can easily extract them.

Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int max_type_factor = 1000;

#define TYPE_ENUMERATOR(d, q) D ## d ## Q ## q = (d) * max_type_factor + (q),

enum Type
{
	TYPE_ENUMERATOR(1, 3)
	TYPE_ENUMERATOR(2, 9)
	TYPE_ENUMERATOR(3, 19)
	TYPE_ENUMERATOR(3, 27)
};

int get_dimension(Type t)
{
	return t / max_type_factor;
}

int get_factor(Type t)
{
	return t % max_type_factor;
}
Last edited on
Unscoped enums make it easy to get the underlying numeric value of the enumerated constant, they implicitly convert to integral values:
1
2
3
4
5
6
7
8
#include <iostream>

enum Type { D1Q3, D2Q9, D3Q19, D3Q27 };

int main()
{
   std::cout << D1Q3 << ' ' << D3Q27 << '\n';
}
0 3

https://www.learncpp.com/cpp-tutorial/unscoped-enumeration-input-and-output/

Scoped enums take a bit of a nudge to suss out the underlying value:
1
2
3
4
5
6
7
8
9
#include <iostream>

enum class Type { D1Q3, D2Q9, D3Q19, D3Q27 };

int main()
{
   std::cout << static_cast<int>(Type::D1Q3) << ' '
             << static_cast<int>(Type::D3Q27) << '\n';
}
0 3

https://www.learncpp.com/cpp-tutorial/scoped-enumerations-enum-classes/
Thanks for your answer. I'm looking for something just for example take D2Q9 and return the dimension=2 and number node=9. I thought about first option before but you need to repeat it twice to get dimension and node. maybe I can use pair for the fist option.
Last edited on
Cplusc wrote:
maybe I can use pair for the fist option

Or define your own struct type for more descriptive names than first and second.
thanks for the help.
basically you are asking for a variable name via code.
you don't normally do this:

int ab12hoopajoo{42};
cout << //what now, I want to peel the number 12 from the variable name here, how?

you can do this via preprocessor macros, but its a horrible solution and usually an indication of a bad idea.
instead, consider a map or similar structure that ties the name as a string and the value together.
Yes. Enum is named values, similar to constant variables.
1
2
3
4
5
6
enum Type { D1Q3, D2Q9, D3Q19, D3Q27 };
// or
constexpr int D1Q3  = 0;
constexpr int D2Q9  = 1;
constexpr int D3Q19 = 2;
constexpr int D3Q27 = 3;

In both cases you can refer to literal constant 2 with name D3Q19 in your code.

The names in the code are only for your convenience; they do not exist in running executable.
This is exactly the sort of thing that wise_enum helps with:
https://github.com/quicknir/wise_enum
I really do not understand why the language is still missing simple enum-string conversion. It's so much simpler to solve this in the compiler than in a library.
Topic archived. No new replies allowed.