Hacker News new | ask | show | jobs
by o11c 906 days ago
You don't need multimethods per se, but you need something and `dynamic_cast` is usually easiest (and with reasonable restrictions, most efficient).

Overloaded operators is a major category of problem here. The "which subclass (if any) is more derived" might be done by the compiler proper, but that still needs to use the cast internally. And of course if you ignore operator overloading, you're just pulling a Java and mandating extra verbosity; the user's problem still has to be solved the exact same way.

(most other use cases for multimethods I don't find compelling)

1 comments

Successful dynamic_casts are fast. But failing dynamic_casts usually involve at least one strcmp and are extremely expensive.

https://gcc.gnu.org/legacy-ml/gcc-patches/2009-07/msg01239.h...

For the particular set of tradeoffs made by C++. When rolling your own you don't have to do make the same choices.