Do both parentheses and braces around expressions basically do the same thing?

137 views Asked by At

Simply put, are these two for cycles functioning the same way:

for (int i = 0; i < (p_size < size ? p_size : size); i++);
for (int i = 0; i < {p_size < size ? p_size : size}; i++);

?

The loop is inside a method (member function), p_size is its parameter and size is an attribute (member variable). Microsoft Visual Studio 2015 compiles both codes, but p_size is not coloured like the other parameters (in the editor) in the code with the curly brackets.

1

There are 1 answers

0
Bill Lynch On BEST ANSWER

This is valid code:

for (int i = 0; i < (p_size < size ? p_size : size); i++);

This is invalid code:

for (int i = 0; i < {p_size < size ? p_size : size}; i++);

Having curly braces in the middle of the expression like that is invalid.

I'd also in general recommend std::min:

for (int i = 0; i < std::min(p_size, size); i++);