5 ms·
> The huge and important difference is when you use this pattern in C the compiler has no idea what you are doing and can't check anything Function pointers ex
by Jasper_ 6y ago
> The huge and important difference is when you use this pattern in C the compiler has no idea what you are doing and can't check anything
Function pointers exist in C and are type-checked by the compiler just fine.
> A compiler for a language where this is a feature can check what you are doing and de-virtualize virtual dispatch.
Virtual dispatch is pretty much only used in the kernel in places where virtualization is necessary. Devirtualization is pretty rare in C++ in practice (I don't believe I've ever seen a "virtual final" method, and my day job is on a large C/C++ codebase).
- jeffbee 6y ago> Function pointers exist in C and are type-checked by the compiler just fine. This amount of type safety is almost useless. Many of the function prototypes, for example in inode and dentry ops, have the exact same signature. If you accidentally swap inode.rmdir with inode.unlink, compiler isn't going to say anything. And it won't be caught in code review either, to the extent that Linux even has code review culture. > I don't believe I've ever seen a "virtual final" method, and my day job is on a large C/C++ codebase It's common in my experience, for performance-sensitive code.
- saagarjha 6y agoYou don't need "virtual final" for devirtualization, the compiler will speculatively devirtualize in some cases.