14

I have the following code written in both C++ and C#

 int i=0;
 ++i = 11;

After this C# compiler brings an error

The left-hand side of an assignment must be a variable, property or indexer

But C++ compiler generate this code with no error and I got a result 11 for value of i. What's the reason of this difference?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
shift66
  • 11,760
  • 13
  • 50
  • 83

4 Answers4

42

The difference is that pre-increment operator is lvalue in C++, and isn't in C#.
In C++ ++i returns a reference to the incremented variable. In C# ++i returns the incremented value of variable i.
So in this case ++i is lvalue in C++ and rvalue in C#.

From C++ specification about prefix increment operator

The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The value is the new value of the operand; it is an lvalue.

P.S. postfix increment operator i++ isn't lvalue in both C# and C++, so this lines of code will bring error in both languages.

 int i=0;
 i++ = 11;
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
6

Note that ++i = 11 invokes undefined in C++03 because you are modifying i twice without an intervening sequence point. It is well defined in C++11, however: first the increment, then the assignment.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
5

C# and C++ are 2 totally different languages and concepts. They only share the name because their syntax is based on C. So actually "why this works on C# but not on C++" makes no sense as a question. It's the same as saying why a table is called "table" in English, but "mesa" in Spanish. Because it was decided to be that way.

C# simply does not allow such syntax.

In C++ you're allowed to: first ++i is computed, which makes i = 1, and then 11 is assigned to i, which makes i = 11.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 3
    That's a bit harsh. The question *is* common to the C language and many things that work in C++ also work in C#! It's a perfectly valid question IMO, particularly since in C++ the code in question represents a statement that makes no logical sense. – adelphus Mar 01 '12 at 14:52
  • Harsh? Why harsh? I'm just stating that C# and C++ have nothing in common except being based on C. The question is not common to C, because he's not asking about C, and in C that sentence works perfectly, and it does not in C#, which just demonstrates C# has nothing to do with C except the name and the syntax (which btw is copied by a lot of language like Perl, Java and such). – m0skit0 Mar 01 '12 at 15:02
  • 2
    C# and C++ have similarities... There aren't different like for example Java and Haskell. So for me it's a valid question and it's interesting for me to know. – Chuck Norris Mar 01 '12 at 19:39
  • They're different like Java and Haskell, but I'm not going to state all the differences and the very few similarities. It's indeed interesting to know, but I personally wouldn't have formulated the question that way. – m0skit0 Mar 06 '12 at 14:53
2

the semantics are very different. In c++ the semantics are that you assign the value 11 to the storage location identified by i. In C# the semantics are equal to the semantics of the below statement

1=11

That is it's equal to trying to assign the value 11 to the value 1 which the C# compiler does not allow. (Fortran compilers actually do allow this and that can create hellish debugging scenarios)

Rune FS
  • 21,497
  • 7
  • 62
  • 96