There are different approaches for what you want to do and each one has its own requirements and drawbacks.
Solution 1: The user knows at compiletime what the serialized data looks like
Just write a template function that takes a type and try do deserialize into it. Check the runtime type info of the binary blob and throw a exception if the type does not match.
Solution 2: User knows the set of types that are possible, std::variant approach:
Make use of the visitor pattern. You can look at the interface of variant for details.
Solution 3: The user has no clue of what types are to come:
You are pretty much out of luck here. You need to make something like std::any which is in its essence a glorified void*.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…