• 0 Posts
  • 13 Comments
Joined 1 year ago
cake
Cake day: July 22nd, 2023

help-circle







  • I think of std::any as a void* that retains type info.

    A typical use case for void* is user data in callback functions. If you’re writing a library that offers callbacks to client code, you may want to provide a way for the user to pass along their own data when registering a callback. Then when calling it, you return that data unmodified*. The library doesn’t know nor care what this user data is. Since the days of K&R C, this has been done with void*.

    But void* erases the type. The library may not care about the type, but the client code does. The only way to get the original type from a void* is an unsafe cast. std::any mitigate this.

    *edit: unmodified, not modified!