Is it possible to have polymorphic behaviour without the cost of virtual functions?
Show answer and explanation
Yes. Templates, CRTP, concepts, and discriminated unions such as std::variant can provide static or closed-set polymorphism without virtual dispatch. The trade-offs include compile-time coupling, code size, and less runtime extensibility.
Yes. Templates, CRTP, concepts, and discriminated unions such as std::variant can provide static or closed-set polymorphism without virtual dispatch. The trade-offs include compile-time coupling, code size, and less runtime extensibility.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.