5 ms·
What is the semantic pitfall here?
by edflsafoiewq 5y ago
What is the semantic pitfall here?
- beached_whale 5y agoFor almost everyone, none. delete doesn’t remove the method from the overload set, it leaves it there in order to give the caller an error saying don’t do that.
- klyrs 5y agoIt also prevents the compiler from emitting a default implementation for the deleted method.
- tialaramex 5y agoC++ has a keyword, "delete" which originally refers to an operator that destroys objects, thus calling their destructor and giving back resources (most obviously memory) allocated for their lifetime. But, adding keywords to C++ is expensive. You can't use these as symbols, you can't name a class, a variable, a function or anything "delete" because that's a keyword and so it's reserved. A new keyword would clobber existing symbols, and that's painful, so, C++ tries not to do it, instead repurposing existing keywords. So as well as the operator named "delete" now "delete" refers to this feature where you can tell the compiler that this particular overload mustn't be used, if it was looking for an overload, it won't use this because you explicitly said not to (even if it could have otherwise conjured a default) and if somebody else called it by mistake now they get a compile error saying not to. I don't think they would naturally have written =delete if not for being conscious that doing so is "free" (the word is already reserved for something else) whereas some other word would have caused compatibility problems.
- edflsafoiewq 5y agoWhat, you think the syntax is funny? I guess, but that's not a semantic pitfall.
- tialaramex 5y agoPreviously delete referred to an operator Today delete is both the operator and this entirely unrelated feature, so that alters the meaning, a matter of semantics. Natural languages have plenty of such ambiguity, but C++ has a Committee (yes I know about French, no the Académie Française doesn't actually get to decide how French works, that's not how natural languages work) which could have chosen to use a different word and did not.
- deleted 5y ago[deleted]
- edflsafoiewq 5y agoA semantic pitfall would be something where you think the code means one thing (a patch of grass) but it really means another (you fall into the spikes). =delete is neither; it is not possible to confuse it for operator delete, and I don't see even a hole to get your foot stuck in, let alone any spikes.
- tialaramex 5y agoFair. I wouldn't have chosen the phrase "semantic pitfall", however after some thought I think it would classify as a "hole to get your foot stuck in" because it's unexpected in an artificial language to have this. If you're a learner and you covered "delete" the operator last week, then missed a day, and now there's = delete on the slides, do you raise your hand to ask what's this completely new syntax? No, right, it must somehow be the delete operator you learned about, as otherwise it would have a different name... wouldn't it? But sure, C++ does have actual hidden spike traps, where unwary programmers are going to hurt themselves badly by mistake and this is not one of those.
- squeaky-clean 5y ago> it is not possible to confuse it for operator delete, I haven't written C++ in many years and my interpretation of the code examples before reading the rest of the article was that it was somehow binding it to the delete operator as a function.
- cesarb 5y ago> A new keyword would clobber existing symbols, and that's painful, so, C++ tries not to do it, instead repurposing existing keywords. It's even more painful than that. A new keyword could conflict with a macro. So even contextual keywords (something which is a keyword only in a place where arbitrary symbols are not allowed, so there's no conflict) could be problematic.