原文
| ||||||||||
| ||||||||||
![]() |
原始链接: 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) 的修复也受到了好评。
| ||||||||||
| ||||||||||
![]() |
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:
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.
reply