2

Out of curiosity, why the bitwise operator ~ is the only non-logical operator that does not have an assignment version in C++, i.e. ~=? All relational and bitwise operators have an assignment version, but ~= is missing. Is there a particular reason for that?

For all the operators in C++, see for instance: http://www.tutorialspoint.com/cplusplus/cpp_operators.htm

RAs
  • 377
  • 3
  • 13
  • 13
    It's a unary operator...What would that even mean? – David G Aug 13 '16 at 17:48
  • 1
    *Binary* operators natuarelly allow for a compund-assigment version. But how would you provide a compund-assigment version for a *unary* operator??? `~` is unary. For example, unary `-` is non-logical and it does not have an assignment version either. – AnT stands with Russia Aug 13 '16 at 17:50
  • 3
    I dont' get why the downvotes. This is a legitimate question, a curiosity about the functionings of the language. – RAs Aug 13 '16 at 17:51
  • 2
    `This is a legitimate question` No, it is not. It doesn't make sense for unary op – Severin Pappadeux Aug 13 '16 at 17:52
  • 2
    @SeverinPappadeux If that is the case, it does not make the question non-legitimate. A good answer could just explain that and why that is the case. If an user was to know things in advance, an user wouldn't ask a question. – RAs Aug 13 '16 at 17:53
  • SO has "newest", "frequent", "votes", "active" tabs to sort the questions. Now we need a "controversial" tab same way as it is on reddit. Don't close this question! – Ivan Aksamentov - Drop Aug 13 '16 at 18:05
  • It does. `A op= B` is for binary ops, and requires TWO operands, A and B, no matter what. This is definition of the binary op - some interaction between TWO arguments, You might ask as well why, say, binary op doesn't have 75 arguments. Well, it is a binary op, it requires two, no less, no more... – Severin Pappadeux Aug 13 '16 at 18:07
  • @SeverinPappadeux "why, binary op doesn't have 75 arguments" and this is still a valid question on SO, that has a definitive answer. Also OP didn't know about binary vs unary operators – Ivan Aksamentov - Drop Aug 13 '16 at 18:08
  • Legitimate question would be what if we introduce BINARY ~ op with this or that semantics, and then ask to have it with `~=` syntax. That might be good question to ask – Severin Pappadeux Aug 13 '16 at 18:09
  • @Drop I would repeat myself, but: In my humble opinion legitimate question would be what if we introduce BINARY ~ op with this or that semantics, and then ask to have it with `~=` syntax. That might be good question to ask – Severin Pappadeux Aug 13 '16 at 18:10
  • 1
    @SeverinPappadeux "what if we introduce BINARY ~ op" This is a good question for Standardization Committee, but an offtopic for StackOverflow – Ivan Aksamentov - Drop Aug 13 '16 at 18:10
  • When you ask a question think about what is your desired outcome. Obviously there is no use in a `~=` operator, it doesn't make any sense as stated. I would also argue that you didn't think it through before posting this question, Moreover, even if it did make sense, this question is (most of the times) bad - `why doesn't C++ have this X feature?` because it doesn't, this is the standard. – Xiobiq Aug 13 '16 at 18:11
  • @Polikdir That's absurd: then an OP should just have to edit the question for "why is it in the standard". That's the kind of view that worsened SO over the years. See for instance the following question from years ago: http://stackoverflow.com/questions/6346001/why-are-there-no-or-operators?rq=1 – RAs Aug 13 '16 at 18:16
  • @Drop no, it is not an off-topic, there are special tags like `language-design` and `language-lawyer` for such questions – Severin Pappadeux Aug 13 '16 at 18:19
  • @SeverinPappadeux I think what Drop meant was not that your suggestion was unsuitable due to lacking tags, but because of being discussion-based (more suitable for a forum), instead of having a clear specific answer. While mine, as nonsensical as you may find it, is a question with a direct specific answer. – RAs Aug 13 '16 at 18:21

1 Answers1

9

All operators from which compound assignments are made are binary. Tilde, on the other hand, is unary, so there is no easy way to make a compound assignment from it, because there is nothing to put on the right-hand side.

Other unary operands, such as unary minus and logical NOT ! operator, also do not have compound assignments.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @DimChtz That is exactly what got me wondering about the `~=` case and how and if it could have an assignment operator. – RAs Aug 13 '16 at 17:57
  • 1
    @DimChtz That's what the answer said - there is no compound assignment based on the `!` (the inequality operator is something completely different). – Sergey Kalinichenko Aug 13 '16 at 17:58