Is there an adequate way in C++ to extract a hash from the data that std::any stores? Well, or at least an object in the form of a list of bytes and its length
Is there an adequate way in C++ to extract a hash from the data that std::any stores? Well, or at least an object in the form of a list of bytes and its length
std::any is a type-safe mechanism for passing an object of known type from one location to another, through an intermediary that does not need to know what that type is. Computing a hash from it is not its goal. And indeed, it wouldn't be meaningfully possible without compromising any's functionality.
Hashing an object requires some knowledge of what that object is and is doing. Assuming that you can just look at the bytes of the object representation and thereby compute a meaningful hash from it is not going to end well. It might appear to work... for a while. But eventually, it's going to do the wrong thing.
You could create a type-erased type similar to any that requires the object to implement hashing. But std::any is not that type, because anyone who doesn't want to hash the types they put into any would be unable to store said object in any.
This is because any operation that any provides is an operation that all types that gets stored into any must also provide. For example, any is copyable, therefore any cannot store move-only types. That is an annoyance for those who want to do so, and the more functionality you dump into any, the more restrictive the type's ability to store "any"thing becomes.