Live data from Hacker News

C++20 Improved the For-Loop Syntax

lzon.ca

51–60 of 89 posts

Re: C++20 Improved the For-Loop Syntax

#51

> It seems to me that the C++ Standards Committee is doing a decent job maintaining the language, and introducing useful features when it makes sense to do so. This can’t be further from truth. C++ is essentially Frankenstein’s monster.

I believe one of the main goals of whatever shadowy organizations are behind the "standards committee" is to sabotage the language to help clear the way for Rust. If these people really have a great idea for what the future C++ should be like, then they should introduce that language all at once. Let us see their grand vision in one big package that can be embraced or rejected. This steady drip-drip-drip of half-bake…

>do you see any reason that std::optional max; couldn't just be a regular uint8_t?

It's so that

    if (max)
is truthy iff max has been assigned (including the case where it's been assigned zero).

I don't know if it's possible for max to be assigned zero in this specific context, or what the correct behavior is if max==0. However, the good thing about this code is that it clearly communicates that the intended check is 'has been assigned to' rather than 'is nonzero'.

The function, for reference:

    void WasmShuffleAnalyzer::TryReduceFromMSB(OpIndex input,
                                               const Simd128ShuffleOp& shuffle,
                                               uint8_t lower_limit,
                                               uint8_t upper_limit) {
      DemandedBytes demanded = GetDemandedBytes(&shuffle);
      std::optional max = {};
      for (unsigned i = 0; i = lower_limit && index (index % kSimd128Size),
                         max.value_or(uint8_t{0}));
        }
      }
      if (max) {
        // input can be reduced.
        TRACE("Can reduce Op %d based upon max used index: %d\n", input.id(),
              max.value());
        demanded_byte_analysis_.Add(
            input, DemandedBytes::LowFromMaxShuffleIndex(max.value()));
      }
    }

Re: C++20 Improved the For-Loop Syntax

#53
post #5

Just what C++ needs, more bloat lol.

C++ is probably the only language that needs a strong style guide, to define a subset you are allowed to use in the project. Linus Torwalds famously said that subset is zero. You're not allowed to use C++ in Linux, a wise move. Here's Google's: https://google.github.io/styleguide/cppguide.html Search for "do not use" and you'll find plenty of hits.

Perl is another example. It has a dedicated linter to detect the myriad ways of using it wrong.

Re: C++20 Improved the For-Loop Syntax

#54
post #42

Reading this, I really can't make much sense of it: for (int i=0; auto&& it: vec) cout It's certainly not obvious what's going on there at a glance. This is at least a bit more pythonic: for (auto [i, it] : std::views::enumerate(vec)) { std::cout

The part that confuses me is that they use “it” to name a variable that is not actually an iterator. That’s very bad style. I would name it “s”.

No argument that it's inappopriate, but I wonder if the author works with other programming languages - in some (Groovy, Kotlin, Ruby), "it" is the implicit block parameter.

Re: C++20 Improved the For-Loop Syntax

#55
post #42

Reading this, I really can't make much sense of it: for (int i=0; auto&& it: vec) cout It's certainly not obvious what's going on there at a glance. This is at least a bit more pythonic: for (auto [i, it] : std::views::enumerate(vec)) { std::cout

The part that confuses me is that they use “it” to name a variable that is not actually an iterator. That’s very bad style. I would name it “s”.

It's "it" as in the pronoun, not as in "iterator".

Re: C++20 Improved the For-Loop Syntax

#56
post #14

Only people who saw nothing bad in passing pairs .begin(), .end() in tons of places in c++ for like 30 years can say that thing like ‘auto&&’ improves anything.

What exactly is your complaint with it? `auto&&` has been standard syntax in C++ for going on 15 years now and has a very clear, irreplaceable meaning - a type-deduced forward reference - to any remotely competent developer.

I think C++ needs more of `:`, `::` and maybe new operators `&&&` and `&&&&`.

Re: C++20 Improved the For-Loop Syntax

#57
Even more modern, we are getting C++26 nowadays.

CTADs for vector (no need for explicit string), ranges enumeration, and range-based for-loop with structured types

    int main() {
        vector vec = { 
            "the", "quick", "brown", "fox", 
            "jumped", "over", "the", "lazy", "dog" 
        };

        for (auto&& [i, it] : vec | std::views::enumerate)
            println ("{}:{}", ++i, it);
    }
Compiler explorer example, https://cpp.godbolt.org/z/3r8rPdjbG

Re: C++20 Improved the For-Loop Syntax

#58

Reading this, I really can't make much sense of it: for (int i=0; auto&& it: vec) cout It's certainly not obvious what's going on there at a glance. This is at least a bit more pythonic: for (auto [i, it] : std::views::enumerate(vec)) { std::cout

It certainly is easy, for anyone that learnt other OOP languages like Smalltalk and Object Pascal before coming into C++, as I did back in the day.

Re: C++20 Improved the For-Loop Syntax

#59

C++20 also has an enumerate() generator, so if you like the python syntax you can just do: for (auto [i,v] : std::views::enumerate(vec)) std::cout FWIW C++23 also has a python-like print and println: std::println("{}: {}", i, v);

> std::cout C++ needs to abandon iostreams. Didn't the C++ community acknowledge that it was a bad idea? In the early days of D, people did want to do a version of it for D, but I objected and currently nobody wants it.

Some folks don't like iostreams, I keep using them, since Turbo C++ 1.0 for MS-DOS.

Although I would probably use std::print in more modern compilers.

D needs to work into its marketing, all key features that made it relevant back in 2011 with Andrei's book have now been copied even if badly, across all mainstream languages.

Re: C++20 Improved the For-Loop Syntax

#60

> It seems to me that the C++ Standards Committee is doing a decent job maintaining the language, and introducing useful features when it makes sense to do so. This can’t be further from truth. C++ is essentially Frankenstein’s monster.

All mainstream languages that survive 1.0, have a bit of Frankenstein into them.
Post reply on HN