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.
The above is the detailed content of How Can C Achieve Type Erasure: A Comparison of Techniques?. For more information, please follow other related articles on the PHP Chinese website!