54 ms·
I was talking generally, not for this particular feature. But it would still be sort of arcane knowledge for me since when I last actively used C++ `=delete` ei
by simias 5y ago
I was talking generally, not for this particular feature. But it would still be sort of arcane knowledge for me since when I last actively used C++ `=delete` either didn't exist or was not in common use, so if I encountered it in some codebase I'd probably guess what it does but still have no idea about the implications.
Like, are there other =<something> construct? Is it overridable or overloadable by the user? Can I use it to "erase" any member of a class, for instance to hide a parent member from an inherited class?
I suspect that the answer to all of these questions is "no", but I can't know for sure without deep diving into some C++ ref.
In the C++ I knew there was nothing resembling this syntax, and `delete` was only the keyword you used to free memory.
- cesarb 5y ago> Like, are there other =<something> construct? Yes, there's "= 0" to mark a virtual method as not implemented in this class (that is, it must be implemented by a subclass), which is older than "= delete". Conceptually, it sets the corresponding slot in the virtual method table to null instead of a pointer to the (non-existent) method, except that it actually doesn't do that, it sets that slot to a pointer to a "pure virtual method called" function from the standard library, which AFAIK prints an error message and exits the program (IIRC, this is because there are some situations in which you can actually manage to call such a pure virtual method, like calling it from a function called within the constructor or destructor of the base class.)
- jdsully 5y agoA concrete object must implement all virtual methods including those marked as pure virtual. All =0 means is that someone else lower down the inheretence chain will implement this and the caller using the class type can rely on it existing. If you try to instantiate an object without all methods implemented you'll get a compiler error about the class being "abstract". You should never have a case where there's a null in the vtable. If there is some specific edge case around ctors then forgive me - this is C++ after all! But in practice you don't have to worry about this which is true for most of the C++ minutia.
- cesarb 5y ago> If there is some specific edge case around ctors then forgive me - this is C++ after all! Yes, the specific edge case is around ctors and dtors. During the ctor or the dtor, the vtable is the vtable of the base class, not the derived class, so if you call a virtual function within the ctor or dtor of the base class (don't do that), the function which will be called is the one from the (possibly abstract) base class. See for instance this FAQ entry: https://isocpp.org/wiki/faq/strange-inheritance#calling-virtuals-from-ctors https://isocpp.org/wiki/faq/strange-inheritance#calling-virt...
- kllrnohj 5y agoI would argue that's just "knowledge" not "arcane knowledge". To me arcane knowledge is stuff like the xvalues, prvalues, etc... Where it's both complex and obscure. Where you can go down a headache-inducing rabbit hole. This isn't really that, it's pretty simple, and really only does that single thing. It doesn't have complex interactions with anything else: https://en.cppreference.com/w/cpp/language/function#Deleted_functions https://en.cppreference.com/w/cpp/language/function#Deleted_... As a sibling mentioned, it's not even a new syntax as '= 0' has always been there.
- gpderetta 5y agoIn addition to =0 mentioned elsethread, there is also =default which will force the generation of a default function that would be otherwise suporesed. You can use =delete to delete any overload of a function, be it class member or namespace scope. Languages evolve; =delete and default were added to get rid if a bunch of common hacks and the language is better for that (at least for delete, default has a bunch of pitfalls of its own unfortunately).