Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 318
Stefan Stanković Avatar asked Feb 08 '23 14:02

Stefan Stanković


1 Answers

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++);
like image 104
Bill Lynch Avatar answered Apr 20 '23 00:04

Bill Lynch