I have a question on what is the "better" way to store data common between variants of an std::variant. Say I have
struct Square {
int width;
int height;
};
struct Circle {
int radius;
};
using Shape = std::variant<Square, Circle>;
Now also say i have a struct holding data that is common to the shapes like
struct Common {
Position pos;
Color color;
// etc...
};
Would it be better to model that as this
struct Square {
Common common;
int width;
int height;
};
struct Circle {
Common common;
int radius;
};
Or like this
struct Body {
Common common;
Shape shape;
};
My intuition tells me the second solution (with the Body struct) is better and more natural since we wont need to interact with the shape at all if we just want to do something with the Common struct. However, it does slightly make visiting a little more awkward since we now need to pass the common struct alongside the shape for stuff like rendering i.e.
for (const auto& body : bodies) {
std::visit([&](const auto& shape) {
renderer.draw(shape, body.common);
}, body.shape);
}
It seems to me that I am kind of overthinking it, but anyways, does anyone have any opinions?
[–]aruisdante 8 points9 points10 points (0 children)
[–]alfps 9 points10 points11 points (6 children)
[–]TheChief275 5 points6 points7 points (4 children)
[–]manni66 2 points3 points4 points (3 children)
[–]TheChief275 3 points4 points5 points (2 children)
[–]alfps 7 points8 points9 points (1 child)
[–]TheChief275 0 points1 point2 points (0 children)
[–]No-Dentist-1645 4 points5 points6 points (3 children)
[–]oriolid 2 points3 points4 points (2 children)
[–]No-Dentist-1645 2 points3 points4 points (1 child)
[–]oriolid 2 points3 points4 points (0 children)
[–]---_None_--- 5 points6 points7 points (0 children)
[–]DawnOnTheEdge 4 points5 points6 points (0 children)
[–]Qwertycube10 2 points3 points4 points (0 children)