0

I stumbled upon this syntax while revising for my final exams in Informatics.

 int x = (1, 2, 3, 4, 5);

What exactly does this represent? Is it an array or something else? I don't usually code in C++, but I have the basic knowledge of its syntax and I never saw this syntax before.

kesetovic
  • 144
  • 6
  • where did you find this? Its an ordinary `int` called `x` with a confusing initializer that uses the comma operator. – 463035818_is_not_an_ai Jun 10 '22 at 13:04
  • 3
    The `(1, 2, 3, 4, 5)` yields `5`. The other expressions are evaluated, then discarded. (If they have side-effects, the side-effects will happen, in the correct order.) – Eljay Jun 10 '22 at 13:04
  • Can you give me the name of this particular way of declaring variable, so I can search it up more? – kesetovic Jun 10 '22 at 13:05
  • 1
    fwiw, when a declaration starts with `int x = ..` then its an `int` not anything else – 463035818_is_not_an_ai Jun 10 '22 at 13:05
  • there is no name for this particular use of the comma operator because one would not write it like this. The line should not pass a code review. – 463035818_is_not_an_ai Jun 10 '22 at 13:06
  • @463035818_is_not_a_number well aware of that, but is it an int array, or something else, how does it work and what value does it produce? – kesetovic Jun 10 '22 at 13:06
  • 1
    You may be interested in the hour long presentation [The Nightmare of Initialization in C++](https://www.youtube.com/watch?v=7DTlWPgX6zs) by Nicolai Josuttis at CppCon 2018. – Eljay Jun 10 '22 at 13:06
  • see the duplicate. Its an `int` not an int array or something else. – 463035818_is_not_an_ai Jun 10 '22 at 13:07
  • 3
    It's the same as writing `int x = 5;`, but written in an intentionally confusing way – CoffeeTableEspresso Jun 10 '22 at 13:22
  • If you encounter something like this on an actual exam, then it's a terrible exam question. You would *never* see code like this in the wild. It's a question that only exists to confuse, not to demonstrate actual knowledge about C++. – Silvio Mayolo Jun 10 '22 at 14:01

0 Answers0