Implement a type-erased function wrapper
Implement a Type-Erased Function Wrapper
Implement a minimal std::function-like callable wrapper in C++ using type erasure via inheritance and a virtual call. Define a template Function<R(Args...)> that can store and invoke any callable (function pointer, lambda with captures, or functor). The wrapper should support copy/move construction and assignment, operator(), destruction, and a boolean conversion for an empty state. You may ignore small-buffer optimization and heap/memory constraints. Explain your exception-safety choices and the time/space costs of invocation and copies compared to the standard library version.
AI interview prep
Core dilemma
Balancing the flexibility of storing arbitrary callables with the performance overhead of type erasure and virtual dispatch, while ensuring correct state management and exception safety.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a type-erased function wrapper
Implement a Type-Erased Function Wrapper
Implement a minimal std::function-like callable wrapper in C++ using type erasure via inheritance and a virtual call. Define a template Function<R(Args...)> that can store and invoke any callable (function pointer, lambda with captures, or functor). The wrapper should support copy/move construction and assignment, operator(), destruction, and a boolean conversion for an empty state. You may ignore small-buffer optimization and heap/memory constraints. Explain your exception-safety choices and the time/space costs of invocation and copies compared to the standard library version.