-2

A developer was copying and pasting similar types of code and made a mistake where instead of

int x = 100;
int y = 100;
int z = 100;
aFunction(x, y, z);

They accidentally typed

int x = 100;
int y = 100;
int z = 100;     
(x, y, z);  //What does this Line of Code do?

This code successfully compiled and gave no warnings. When trying to debug this code in VS 2015 the debugger will just skip over this line of code.

Why does this line of code compile and give no warning and what is this line of coding doing?

Update: Building in Visual Studio 2015 with all warnings shows this warning but level 4 does not. Strangely, neither Code Analysis nor Clang Power Tools shows this warning.

Thanks.

Bogdan Mitrache
  • 10,536
  • 19
  • 34
Donald Herman
  • 314
  • 1
  • 5
  • 15
  • 2
    _`// What does this Line of Code do?`_ Nothing. – πάντα ῥεῖ Aug 03 '18 at 16:59
  • 3
    `(x, y, z);` is not a function call. Parentheses are used for other things as well – UnholySheep Aug 03 '18 at 17:00
  • 2
    Read about [the comma operator](http://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator). – Some programmer dude Aug 03 '18 at 17:00
  • 1
    Interestingly, I think this might be the one usage not described in this question: [Different meanings of parentheses in C++?](https://stackoverflow.com/q/5048921/691711). – zero298 Aug 03 '18 at 17:01
  • Go to https://en.cppreference.com/w/cpp/language/operator_other and scroll down to the section that talks about the comma operator. – R Sahu Aug 03 '18 at 17:08
  • 1
    Related: [What does x+1 mean without assignment?](https://stackoverflow.com/questions/41942049/c-what-does-x1-means-without-assignment) – Ben Jones Aug 03 '18 at 17:21
  • In VS the default warning level is relatively low. You can increases the warning level of VS and be warned about this kind of thing (you probably should as all warnings tell you about logical failure in your thinking). – Martin York Aug 03 '18 at 18:02

1 Answers1

7

Why does this line of code compile?

Because that expression is syntactically correct

give no warning and what is this line of coding doing?

That depends on compiler and warning level you set on it. Some compilers warn you if expression does not do anything. For example g++ with -Wall flags gives following:

 t.cpp:4:5: warning: left operand of comma operator has no effect [-Wunused-value]
     (x, y, z);  //What does this Line of Code do?

 t.cpp:4:8: warning: right operand of comma operator has no effect [-Wunused-value]
     (x, y, z);  //What does this Line of Code do?
Slava
  • 43,454
  • 1
  • 47
  • 90
  • Building in Visual Studio 2015 with all warnings shows this warning but level 4 does not. Strangely, neither Code Analysis nor Clang Power Tools shows this warning. – Donald Herman Aug 03 '18 at 18:07
  • @DonaldHerman If you encounter any problem with Clang Power Tools you can open an issue here: https://github.com/Caphyon/clang-power-tools/issues – Ionut Enache Nov 06 '19 at 13:47