I would like to have struct
s holding enum
s for iteration, together with std::string
holding their names for creating menu entries. I was using something like this:
struct S
{
enum E { ONE, TWO, ALL };
std::array<std::string, ALL> names { "one", "two" };
};
int main()
{
S s;
for(int i=s.ONE; i<s.ALL; ++i) // or S::...
std::cout << s.names[i] << '\n';
return 0;
}
As I understand, this is preferred to having global variables. It works, but needs instantiation before usage. Now, I found out about this method, which needs --std=C++17
to compile:
struct S
{
enum E { ONE, TWO, ALL };
static constexpr std::array<std::string_view, ALL> names { "one, "two" };
};
int main()
{
for(int i=S::ONE; i<S::ALL, ++i)
std::cout << S::names[i] << '\n';
return 0;
}
But how will this behave, in terms of memory usage, compared to my previous way of doing this? Or is there a wrong way in how I do it? What would be a better way?