原文
| ||||||||||
| ||||||||||
![]() |
原始链接: https://news.ycombinator.com/item?id=43792348
Hacker News 上的一篇讨论重点关注 GCC 15 中 C++ 的新特性。一个关键点是 GCC 修复了 C++20 中基于范围的 for 循环的问题,但这可能导致可移植性问题,因为它延长了临时向量的生命周期,这与其他编译器不同,后者可能会导致细微的错误。评论者对即使生态系统缩减到三大厂商,编译器可移植性问题仍然存在表示失望。 积极的一面是,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