Suppose I have 2 different structs:
struct A {
char a_name[512];
... // A struct data
}
struct B {
char b_name[256];
... // B struct data
}
Given std::string name, I need to check if an object (be it A or B) exists in a container. As both A and B start with a char[size] member which is the name I'm looking for to compare, I tried the following:
// T is a template parameter which is always an object of type A or B
↓
for (const T& item : container)
if (name == reinterpret_cast<const char*>(&item))
It does work as expected. I read the object as if it was an array of chars, but as A.a_name and B.b_name are guaranteed to be null terminated, I don't read what I shouldn't... or do I? Can the code above produce undefined behaviour? Can it produce any error that I'm not taking into account? If so, how can I then approach this task?
I cannot modify A or B. I chose not to overload operator== for each class because A and B are actually many more different structs which have that structure I described above (char[size] + some more data).