Let's suppose I would like to define a struct which can be seen as a 2D array, but which also provide member like accessors to the content of the array.
For example, one could write a Point2D
struct that would essentialy be an array, but also provide x and y members (not accessors). Such a struct could serve as a bridge between different libraries : I work with some libraries that consider a point as having x a y members and some others which see it as being an array.
Godbolt: https://godbolt.org/z/4H-gae
#include <array>
#include <cassert>
struct Point2D : public std::array<double, 2>
{
using Base = std::array<double, 2>;
Point2D(Base && v) :
Base(v),
x(operator[](0)),
y(operator[](1))
{}
double &x, &y;
};
void test()
{
Point2D p({1., 2.});
assert(p.x == 2.);
p.x += 4.;
assert(p.x == 5.);
}
Is such a use case reasonable or am I asking to be shoot in the foot repeatedly by various UBs?
If such an approach is reasonable, is there a way to make the constructor constexpr?