(评论)
(comments)

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

Hacker News 的一个帖子讨论了 GCC 15 中新的 C++ 特性。一个关键点是 GCC 改进了对 C++20 中基于范围的 for 循环的处理,这可能会延长临时对象的生存期。虽然看似有帮助,但这项修复可能会导致可移植性问题,因为在 GCC 15 中工作的代码可能会在其他 C++20 编译器上由于不同的生命周期管理而中断。评论者对持续存在的编译器可移植性问题表示遗憾,尽管编译器生态系统正在缩小。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)
    36 points by jrepinc 14 hours 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