(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=43792348

Hacker News 上的一篇讨论重点关注 GCC 15 中 C++ 的新特性。一个关键点是 GCC 修复了 C++20 中基于范围的 for 循环的问题,但这可能导致可移植性问题,因为它延长了临时向量的生命周期,这与其他编译器不同,后者可能会导致细微的错误。评论者对即使生态系统缩减到三大厂商,编译器可移植性问题仍然存在表示失望。 积极的一面是,GCC 改进的模块支持使其与 Clang 和 MSVC 保持一致。`#embed` 特性也因简化安装程序创建而受到好评,尽管在 Windows 中使用注册表的相关性仍存在争议。修复基于范围的 for 循环中未定义的行为 (UB) 也是一项受欢迎的改进。

相关文章
  • (评论) 2025-04-10
  • (评论) 2025-04-25
  • (评论) 2025-04-15
  • (评论) 2025-03-30
  • (评论) 2025-03-26

  • 原文
    Hacker News new | past | comments | ask | show | jobs | submit login
    New C++ features in GCC 15 (redhat.com)
    37 points by jrepinc 1 day ago | hide | past | favorite | 6 comments










    > 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 const &identity(vector const &a) { return a; }
    
      for (auto a : identity(vector{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.



    Nothing new unfortunely, when C++11 brought fresh wind into C++ and everyone raced to support it, I thought the compiler portability issues would eventually be a thing of the past.

    Instead, even when the ecosystem has been reduced to three major compilers, and derived forks from two of them, it has hardly changed when writing portable code.

    There are other examples, like supporting C++23 std in C++20 mode, not all of them support it.



    For me one of the big improvements is the modules support, GCC is now joining clang and MSVC, kudos to the team.


    #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.


    At least on Windows you could already do that with resource files, even if not as easy as #embed.

    And although even Microsoft teams themselves aren't that great following their employers advice, the use of the registry should be minimized to the keys that are really required by the system, everything else should be in manifest files, or local configuration.

    Depending on how much you would like to depend on MSI, MSIX, or do your own thing, running a .reg script might also do the job if the entries are rather simple.



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






    Join us for AI Startup School this June 16-17 in San Francisco!


    Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



    Search:
    联系我们 contact @ memedata.com