ev-stdexec-copy-elision-box-init-003¶
| 字段 | 值 |
|---|---|
| 项目 | NVIDIA/stdexec |
| 固定 commit | 6d7ad689f4d4 |
| 路径 | include/stdexec/__detail/__any.hpp |
| 行段 | L481-L489 |
| 特性 | 保证的复制消除 |
| 许可证 | Apache-2.0 |
| 代码来源 | 项目源码 |
| 级别 | manual |
| 审核 | zyuhao7 @ 2026-10-27 |
| 测试代码 | 否 |
| 发现方式 | manual |
用法¶
以函数返回值初始化不可移动的 union 成员,GCC 拒绝强制复制消除时改用 placement new
为什么典型¶
同一规则的另一种回退:用 placement new 在未初始化存储上直接构造,效果等价于复制消除。
摘录¶
// for unknown reasons, GCC (sometimes) doesn't like initializing __val_ with the
// result of this function call in the member initialization clause when _Value
// is immovable even though the conditions should be right to trigger C++17's
// mandatory copy elision, but this in-place new into an uninitialized anonymous
// union member works just fine
static_assert(__same_as<__call_result_t<_Fn, _Args...>, _Value>);
new ((void *) std::addressof(__val_))
_Value(static_cast<_Fn &&>(__fn)(static_cast<_Args &&>(__args)...));
}
备注¶
生产路径;类型擦除的存储包装,注释说明 GCC 有时不实现该处的强制复制消除。