In my program, I have objects (of the same class) that must have a unique identifier. For simplicity and performance, I decided to use the address of the object as an identifier. And to keep the types simple, I use (void*)as a type for this identifier. In the end, I have code like this:
class MyClass {
public:
typedef void* identity_t;
identity_t id() const { return (void*)this; }
}
It seems to work fine, but gcc gives me a warning with strict anti-aliasing. I understand that the code would be bad if the identifier was used to transmit data. Fortunately, this is not the case, but the question remains: will aliases be optimized? And how to avoid the warning?
Note. I do not want to use (char*), as this implies that the user can use the data to copy, which he cannot!
source
share