Reputation: 706
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?
Upvotes: 0
Views: 114
Reputation: 445
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.
Upvotes: 2