Type erasure in C refers to the technique of hiding some or all of the type information regarding a class, similar to Boost.Any. Here are various type erasure approaches:
Virtual functions are a classic technique where you abstract the implementation of a class inside an interface-based class hierarchy. This approach is adopted by many Boost libraries, such as Boost.Any and Boost.Shared_ptr, to hide types or deallocation mechanisms.
An alternative option involves using function pointers with templated functions, while storing the actual object in a void* pointer. Boost.Function utilizes this technique to conceal the real type of a functor.
For instance, GMan's approach utilizes templates to hide actual types without resorting to virtual functions or void* manipulation.
shared_ptr
All type erasure techniques in C employ function pointers and void* pointers, with the primary difference being in the provision of semantic sugar.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3