3 ms·
C++ has a keyword, "delete" which originally refers to an operator that destroys objects, thus calling their destructor and giving back resources (most obviousl
by tialaramex 5y ago
C++ 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.
- jcranmer 5y agoDo you also complain that the operator named '*' is overloaded? As a binary operator, it means to perform a multiplication operator. As an unary operator, it means to load the value located at that memory address, which has nothing to do with anything close to multiplication. That is a more gross alteration of semantics than the use of a keyword in completely different slots (as an expression versus as a declarator).
- tialaramex 5y agoI'm not a fan of your example with the asterisk or of the reference operator sharing a symbol with the binary AND operator. I don't like the use of + for concatenation much either, even though it's overloaded to do that in some languages I really like, and it's even special cased despite the lack of overloading in one language I think is fairly good (Java) and some others I've used but think are garbage. But I'm willing to cut some more slack for symbols because they're short. I think erase for example would have been a better choice here (I haven't spent long thinking about this, the committee had months), but C++ had to worry about the backwards compatibility penalty and that's... sad.
- agent327 5y agoActually it would have been a contextual keyword, meaning it could be any word the committee wanted it to be, and it is only recognized as such in this particular context (i.e. at the end of a function declaration). They could have chosen anything they wanted, but decided that '=delete' was fine. Other examples of contextual keywords are 'final' and 'override', which may also occur on function declarations.
- hnfong 5y agoI guess they mean the "syntax-semantic mismatch pitfall"? :)
- jmull 5y agoA pointless semantic argument about the word semantic... Thank you C++!
- 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.