You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Clang 18.1.8拒绝编译此C++协程代码?

Clang 18.1.8编译C++协程代码报错:operator delete匹配失败

以下代码在Clang主干版本可正常编译,但使用Clang 18.1.8编译时触发operator delete匹配失败错误:

#include <generator>
std::generator<int> foo() {
    int i{};
    while (true) {
        co_yield i++;
    }
}

编译错误信息

/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/generator:612:6: error: no matching function for call to 'operator delete'
  612 |             ::operator delete(__ptr, _M_alloc_size<void>(__sz));
      |             ^~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/new:149:6: note: candidate function not viable: no known conversion from 'unsigned long' to 'const std::nothrow_t' for 2nd argument
  149 | void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPT
      |      ^                      ~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/new:158:6: note: candidate function not viable: no known conversion from 'unsigned long' to 'std::align_val_t' for 2nd argument
  158 | void operator delete(void*, std::align_val_t)
      |      ^                      ~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/new:185:13: note: candidate function not viable: no known conversion from 'unsigned long' to 'void *' for 2nd argument
  185 | inline void operator delete  (void*, void*) _GLIBCXX_USE_NOEXCEPT { }
      |             ^                        ~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/new:135:6: note: candidate function not viable: requires 1 argument, but 2 were provided
  135 | void operator delete(void*) _GLIBCXX_USE_NOEXCEPT
      |      ^               ~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.1.1/../../../../include/c++/14.1.1/new:160:6: note: candidate function not viable: requires 3 arguments, but 2 were provided
  160 | void operator delete(void*, std::align_val_t, const std::nothrow_t&)
      |      ^               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

本地Clang版本信息

$ clang++ --version
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

报错原因

这个问题源于Clang 18.1.8与GCC libstdc之间的兼容性bug:libstdc的<generator>头文件中调用了带大小参数的全局operator delete(void*, std::size_t)重载(该重载是C++17标准允许的),但Clang 18.1.8没有正确识别这个重载,导致编译时无法匹配到对应的函数。而Clang主干版本已经修复了这个适配问题,因此可以正常编译该代码。

内容的提问来源于stack exchange,提问作者Enlico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:05:15