@b0rk sorry but I wont for(uint64_t i=0, i<10; i++) {}

So that explanation lacks context. These examples are significant but largely cherry-picked.

@f4grx @b0rk Nope, sorry, nearly everything about that is off modern C++ spec.

`for (size_t i{0}; i < 10; ++i)` or nobody 'round these parts will sign off on your PR.

@mtomczak @f4grx @b0rk In that kind of loop, is there any benefit to brace initialization of a scalar variable, or preincrement rather than postincrememt? Or are those preferred for some broad reason?

@brouhaha @mtomczak@qoto.org @b0rk for some reason I cant see the toot you are referring to.

@brouhaha @mtomczak@qoto.org @b0rk i believe it's my previous toot with the for loop.

I believe the i++ statement in the for loop init is a tradition. Any loop update statement can be used here, loops dont have to just increment.

The for loop is basically syntactic sugar for a while construct

for(a;b;c) <statement> is equivalent to a;while(b) {<statement>; c}

@f4grx @b0rk Maybe I replied to wrong toot. I understand traditional C loops. I was questioning the intent of mtomczak's C++ style comments, i.e.
int i{0} for initialization and ++i instead of i++.

@brouhaha @b0rk haha I cant see the thing again because I think blork blocked me. I dont think I was *THAT* offensive? whatever.

I{0} looks like a "C++ism" to me. It's invalid in C.

@f4grx @b0rk The braced initializer is definitely a C++ thing, and there may be reasons to prefer it under other circumstances, but for a scalar initializer I think it doesn't do anything but add a tiny bit of obfuscation.
I don't think the toot was entirely serious.

@brouhaha @f4grx @b0rk ++i used to be more efficient than i++ with old compilers/cpus. Totally not a problem anymore with modern stuff.

@ives @f4grx @b0rk I used a lot of old C++ compilers (back to 1982), and was never aware of that. I could see it being different in a subexpression, but it slightly surprises me that it would ever result in different code when it's an entire expression. Even if a compiler was inclined to generate different code, in the case where it's not a subexpression I'd expect even the most rudimentary peephole optimizer to eliminate the difference.

@brouhaha @f4grx @b0rk Definitely one of the more trivial situations to optimize. I suppose it could get a bit more complex in expressions or when overriding operators.
And then there are processors like the Motorola 68000 that have support for post-increment and pre-decrement, but not the others.
But yeah, best to write code that expresses your intention as clearly as possible and let the compiler deal with optimizing it.

@f4grx @brouhaha @b0rk

i++ vs ++i totally depends on what you're trying to do. If you're trying to walk through a 0-indexed array (a common use of for loops in C), preincrementing your index will give you an off-by-one error. They're not interchangeable.

@sethrichards @f4grx @b0rk I disagree. When the increment is a standalone expression, not a subexpression, they are 100% identical.
for (i= 0; i < limit; i++)
has exactly the same semantics as
for (i= 0; i < limit; ++i)
which is the case I was questioning.

Your statement is correct when the increment is a subexpression, so a[i++] is very different than a[++i].

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.