4

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.

Stefan Stanković
  • 628
  • 2
  • 6
  • 17
  • 1
    The version with the braces does not compile with gcc 5.3. It is a non-standard, non-portable, Microsoft compiler-only compiler "feature", apparently designed to "embrace and extend" C++. – Sam Varshavchik Mar 02 '16 at 01:44
  • @SamVarshavchik Don't act like Microsoft is the only compiler vendor "embracing and extending" the language, please. Even open-source compilers do it. – user253751 Mar 02 '16 at 02:00
  • Oh, you happen to know any other compiler that allows you declare main as "void Main(array^ args)", and call _that_C++? – Sam Varshavchik Mar 02 '16 at 02:02
  • At first I thought the construct in the braces was a GCC ["Statement expression"](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html) language extension. But I see that those have to be enclosed in parens and braces: `({ ... })`. Exactly what MSVC extension is this? I'm unfamiliar with it. – Michael Burr Mar 02 '16 at 02:06
  • MS calls `void Main(array^ args)` "C++/CLI" and they have an ECMA standard proposed for it. I don't use it or care much for it (I've not done any real.NET work in a long time, and when I do it's in C#), but one might consider it Microsoft extending C++ for NET in a similar fashion to how Stroustrup extended C into C++ for his purposes. I'm not saying that C++/CLI is a good language or a good idea, but extending an existing language for new needs is not without precedent. One good thing about C++/CLI is that any C++ programmer will immediately recognize it as "not C++". – Michael Burr Mar 02 '16 at 02:18
  • @Stefan: Can you post an complete example including information about command line options/project settings? I get a set of errors starting with `error C2059: syntax error: '{'` when I try to compile the example with the braces in VS 2015. – Michael Burr Mar 02 '16 at 06:07

1 Answers1

8

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++);
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173