Struct with optional members in modern C++ : <p>We have inherited old code which we are converting to modern C++ to gain better type safety, abstraction, and other goodies.  We have a number of structs with many optional members, for example:</p>

<pre><code>struct Location {
    int area;
    QPoint coarse_position;
    int layer;
    QVector3D fine_position;
    QQuaternion rotation;
};
</code></pre>

<p>The important point is that all of the members are optional. At least one will be present in any given instance of Location, but not necessarily all.  More combinations are possible than the original designer apparently found convenient to express with separate structs for each.</p>

<p>The structs are deserialized in this manner (pseudocode):</p>

<pre><code>Location loc;
// Bitfield expressing whether each member is present in this instance
uchar flags = read_byte();
// If _area_ is present, read it from the stream, else it is filled with garbage
if (flags &amp; area_is_present)
    loc.area = read_byte();
if (flags &amp; coarse_position_present)
    loc.coarse_position = read_QPoint();
etc.
</code></pre>

<p>In the old code, these flags are stored in the struct permanently, and getter functions for each struct member test these flags at runtime to ensure the requested member is present in the given instance of Location.</p>

<p>We don't like this system of runtime checks.  Requesting a member that isn't present is a serious logic error that we would like to find at compile time. This should be possible because whenever a Location is read, it is known which combination of member variables should be present.</p>

<p>At first, we thought of using std::optional:</p>

<pre><code>struct Location {
    std::optional&lt;int&gt; area;
    std::optional&lt;QPoint&gt; coarse_location;
    // etc.
};
</code></pre>

<p>This solution modernizes the design flaw rather than fixing it.</p>

<p>We thought of using std::variant like this:</p>

<pre><code>struct Location {
    struct Has_Area_and_Coarse {
        int area;
        QPoint coarse_location;
    };
    struct Has_Area_and_Coarse_and_Fine {
        int area;
        QPoint coarse_location;
        QVector3D fine_location;
    };
    // etc.
    std::variant&lt;Has_Area_and_Coarse,
                 Has_Area_and_Coarse_and_Fine /*, etc.*/&gt; data;
};
</code></pre>

<p>This solution makes illegal states impossible to represent, but doesn't scale well, when more than a few combinations of member variables are possible. Furthermore, we would not want to access by specifying <strong>Has_Area_and_Coarse</strong>, but by something closer to <strong>loc.fine_position</strong>.</p>

<p>Is there a standard solution to this problem that we haven't considered?</p>
