I'm simply trying to create an enumeration of several struct elements with C++ (actually it may look more like C, so if you have any suggestion to make it look more like C++, I'll take it). Here is the code :
struct Vect
{
int x;
int y;
};
enum Direction
{
right = (Vect) {1, 0},
left = (Vect) {-1, 0},
down = (Vect) {0, 1},
up = (Vect) {0, -1}
};
The error I get from g++ is : "enumerator value for ‘right’ is not an integer constant" (and so for the others). Is it even possible to do this ? Otherwise I can find another way to do it, but I found this solution quite elegant.
If all you want is a bunch of constant expressions of type Vect
, you can define them as follows:
namespace Direction
{
constexpr Vect right = { 1, 0 };
constexpr Vect left = {-1, 0 };
constexpr Vect down = { 0, 1 };
constexpr Vect up = { 0, -1 };
}
Vect
as you showed it is a literal type, which is what makes it a possibility. Pretty much why constexpr
was introduced.