Questions tagged [compound-assignment]

For questions about single operators that assign a value based on both a previous value and an operand (e.g., the += operator in C or Python). Also referred to as "augmented assignment." Use this tag if your problem specifically involves or relates to a compound-assignment operator.

Compound assignment operators, such as +=, combine aspects of assignment with aspects of regular binary operators. For example, i += 42 generally has the effect of i = i + 42, but is more concise.

However, that is not always the case. For example, in a Python 3 class, a custom + can be implemented using the __add__ method, and a custom += can be implemented using the __iadd__ method. Those two methods could behave very differently.

102 questions
1
vote
3 answers

When is the right hand expression of bool a |= mayRun(); being executed?

Assume code snippet bool a; a = true; a |= mayRun(); a = false; a |= mayRun(); in which case is mayRun() being executed? All explanations tell me, that a |= b; is equivalent to a = a | b; But it cannot be the same as the example …
Jan
  • 1,042
  • 8
  • 22
1
vote
3 answers

MySet(20,100) = "abcd"; copy and/or assignment?

apologies if this answer should be obvious, perhaps this has a pattern name like "dopey assignment overload" and have not been searching by right topic - redirects most welcome code extracts: class MySet { private: int…
chaosless
  • 573
  • 5
  • 8
0
votes
1 answer

C++ Compound literal

In C i can do this: ppackage ppnull() { return (ppackage) { .type = NULL } } However, in C++ I get syntax errors. I use the GNU g++ compiler. Is there a switch to enable this?
imacake
  • 1,703
  • 7
  • 18
  • 26
0
votes
1 answer

How does the Lua mutate assignment patch work?

I followed this page, and got an metamethod __mutate_asn. This is my test code. local mt = {} mt.__mutate_asn = function(a, b) print(a, b) return a + b end debug.setmetatable(0, mt) a = 1 b = 2 a:=b print(a) output: 1 2 1 "a"…
eale
  • 48
  • 5
0
votes
0 answers

odd behavior with enum struct and compound assignment operator

I observed some behavior relating to enum structs and compound operators that I don't understand (yet). When using an enum struct as a flag, I'd like to write stuff like flag &= enum_name::What. However, depending on the compilation this either…
rochus
  • 1
0
votes
2 answers

Defining two versions of << overload

Can someone tell me what is wrong when I try to overload << in a slightly different way by using another symbol like <<= #include struct Date { int day, month, year, hour, minute, second; Date (int d, int m, int y, int h, int…
0
votes
1 answer

why Binary Operator gives TLE while compound assignment operator is faster?

I am trying to solve Sort Characters By Frequency on LeetCode. The first used Binary Operator gives me a TLE, but when I use a compound assignment operator then it works fine. But I don't understand why. Is there any logic behind it? I'm attaching…
0
votes
1 answer

C# why does assigning an event handler use a += operator and not =

Maybe I'm not googling hard enough, but I'd like to understand the syntax '+=' when assigning an event. For example we use: myButton.Click += MyClickEvent; rather than: myButton.Click = MyClickEvent; Why is this the case? Is there some maths going…
Mark Roworth
  • 409
  • 2
  • 15
0
votes
1 answer

Is multiple compound assignments possible in python

a=5 b=6 a=b+=6 The above statements when executed in python show syntax error. Why is it so? Are multiple compound assignments not possible in python?
0
votes
1 answer

How Does the Compound Modulo Work in C++?

I have been trying to create simple program which divides an input number into peso bills. The output I need is Enter Amount: 7350 P1000: 7 P500: 0 P200: 1 P100: 1 P50: 1 P20:0 P10:0 P5:0 P1:0 and this was my initial code: #include using…
0
votes
1 answer

Assignments on new MultiIndex columns given only level 0 column selectors and aligned level 1

I would like to add a column to the level(0) of a multiindex column dataframe that has the same structure as the other columns, ie: the level(1) index is the same: In [151]: df Out[151]: first bar baz second …
user3673
  • 665
  • 5
  • 21
0
votes
3 answers

How can I add multiple numbers to a variable

I'm trying to use += to add multiple numbers to a variable. I'm trying to do something like this: score += var1, var2, var3 However, the only thing I know how to do now is score += p; score += v; score += t;
user14043389
0
votes
2 answers

Why does compound assignment (+=) differ between languages (Java, C++)?

The definitions of += seem to be the same in both Java and C++, however, they perform differently. Consider the following code in C++: #include int n; int f(int x) { n += x; return x; } int main() { n = 0; n = n + f(3); …
Ethan Yang
  • 48
  • 4
0
votes
1 answer

Compound Assignment in .Net

I wonder if this ever was or still is a case in .Net. Use exp += val instead of exp = exp + val. Since exp can be arbitrarily complex, this can result in lots of unnecessary work. This forces the JIT to evaluate both copies of exp, and many times…
Daniel B
  • 3,109
  • 2
  • 33
  • 42
0
votes
1 answer

MISRA: Compound Assignment operators

I have taken over code that has a lot of compound assignment operators in it. I think that compound operators are not 'really' MISRA compliant. I can't seem to find any reference to them. I believe I understand what is happening and that it should…