Do parentheses and parentheses around expressions basically do the same?

Simply put, these two cycles forfunction the same:

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 inside the method (member function) p_sizeis its parameter, and its sizeattribute (member variable). Microsoft Visual Studio 2015 compiles both codes, but is p_sizenot colored like the other parameters (in the editor) in the code with curly braces.

+4
source share
1 answer

This is a valid code:

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

This is the wrong code:

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

The presence of braces in the middle of an expression like this is not valid.

I would also generally recommend std::min:

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

All Articles