0

I have a scenario in which based on spec name, I want to make members of some struct unreachable. An example follows:

enum schema { IFC_1, IFC_2, IFC_3, IFC_4 };

template <typename T>
using map_type = map <schema, T>;

struct Label
{
    //Imaginary Superset of Label
    //From different specs
    struct data {
        variant <int, char, string> x;
        variant <int, char, string> y;
        variant <int, char, string> z;
        variant <int, char, string> a;
        variant <int, char, string> b;
    };

    map_type<data> abc;
    data & operator[] (const schema & key){
        data & obj = abc[key];
        /*
        if(key == IFC_1)
            then make some members unreachable.
            at compiletime or runtime
        */
        return abc[key];
    }
};

int main(){
    Label x;
    x[IFC_1].x = 0; // all member above are accesible
    x[IFC_2].x = 0; // is it possible to make x unreachable when spec is IFC_2?
    return 0;
}

In the above code, there is the Label struct and it has a std::map taking an enum class as its key and returns a struct that is defined in Label struct. Now based on the key in Label struct, I want to make arbitrary members of struct data unreachable in the main function. Is such thing possible? Can I change access permission of a struct in operator[] at runtime?

Zoe
  • 27,060
  • 21
  • 118
  • 148
nmd_07
  • 666
  • 1
  • 9
  • 25

1 Answers1

2

No you can't. It is syntactically not possible and the access is checked statically when compiling, so it would have no use for you at runtime.

If you want to have some values not present an be able to represent them like that, you can use something like std::optional

Maybe this is helpful fore you to compose the struct statically. This works only if the schemes are statically available.

birdfreeyahoo
  • 445
  • 4
  • 9
  • I want to restrict access to member of `data` based on the schema type. `data` is a superset of all the data members from all the schemes. When a schema is provided, it may not have all the members, so I was looking for a way to restrict access once the spec is provided. – nmd_07 Oct 08 '18 at 22:07
  • If you statically know which scheme it is at one point, you can just use another struct because you then know which to use, or just use the values you want. If not, what do you want to happen? Do you want a compile error? Or do you want the statement to just not be executed at runtime? – birdfreeyahoo Oct 08 '18 at 22:14
  • I didn't want to use another struct because that would be a lot of code duplication instead I just wanted to invalidate from existing ones. Scheme information is statically accessible. What I want to accomplish is when another programmer begins to use this design, I do not want them to access to the members that are not part of a specific scheme version. Above was a superset, but I want users to only access to relevant data members. – nmd_07 Oct 08 '18 at 22:35
  • 1
    Edited my answer – birdfreeyahoo Oct 08 '18 at 22:38