from Hacker News

New C++ features in GCC 15

by jrepinc on 4/25/25, 11:10 AM with 6 comments

  • by tlb on 4/25/25, 12:22 PM

    > Fix for range-based for loops

    Oh man, having different compilers in c++20 mode handle things differently is going to cause more grief, not less.

    Reminder: Prior to c++23 the following is broken:

      vector<int> const &identity(vector<int> const &a) { return a; }
    
      for (auto a : identity(vector<int>{1,2})) { ... }
    
    That's because the lifetime of the vector isn't extended through the life of the for loop. That is, the vector is destructed right after identity returns, and the for loop ends up trying to iterate through a vector that's been destructed.

    But now gcc in c++20 with -frange-for-ext-temps mode will extend the lifetime of the vector and the above code will work, and people will write code like that, and it'll break mysteriously on other c++20 compilers. The usual way it breaks is that the for loop does nothing because in destructing the vector it sets the begin and end pointers to null, so it's a subtle kind of breakage.

    BTW clang with -Wall doesn't complain about the above broken code.

  • by pjmlp on 4/25/25, 11:34 AM

    For me one of the big improvements is the modules support, GCC is now joining clang and MSVC, kudos to the team.
  • by LorenDB on 4/25/25, 12:14 PM

    #embed should make it fairly easy to write an installer from scratch. You'll still have to handle things like registry keys on Windows manually, but the barrier to entry for developing installers will be much lower.
  • by ashvardanian on 4/25/25, 11:41 AM

    `#embed`, finally! Fixing UB in range-based `for` loops is also a good one!