1

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?

Pascal T.
  • 3,866
  • 4
  • 33
  • 36
  • No, such a struct most definitely cannot serve as a bridge between libraries, unless one of the libraries for some ungodly reason uses `Point` with two *reference* members. – n. m. could be an AI Feb 09 '19 at 17:58
  • @n.m : agreed for the type conversion. However; it can work for generic programming : `template double Norm(PointType && pt)` would work for example. – Pascal T. Feb 09 '19 at 18:00
  • A generic library that doesn't provide its own point type but requires that the user-provided one has members named `x` and `y`? I'll pass. – n. m. could be an AI Feb 09 '19 at 18:17
  • "A struct that can be seen as a 2D array". This is a peculiar requirement. Where is this requirement coming from? Otherwise you could have used composition and provided the x, y as references to member std::array. – cplusplusrat Feb 10 '19 at 01:27
  • Aren't you just asking for a union, or am I misunderstanding the question? –  Feb 10 '19 at 02:33
  • [Here](https://stackoverflow.com/questions/54617101/zero-cost-properties-with-data-member-syntax) is one possible approach to the problem. – n. m. could be an AI Feb 10 '19 at 14:42
  • A major issue is that if a Point2D is copied then the references will point to the old copy. It seems that your client code is not wedded to a particular implementation of Point2D. Could you have inline methods X() and Y() that return temporary references into the std::array? – Gem Taylor Feb 12 '19 at 17:04
  • @GemTaylor : You are right, this is a no-go ! I think I'd better opt for methods, although this will require some adjustment on the client code side. My original idea was to create a strong type (https://foonathan.net/doc/type_safe/) on top of an existing implementation from another library. – Pascal T. Feb 13 '19 at 08:16

0 Answers0