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

关于C++23返回局部变量引用报错及局部变量是否为右值的技术问询

关于C++23返回局部变量引用报错及局部变量是否为右值的技术问询

嘿,最近看到Herb Sutter在他的博客里提到,C++23终于把一类老生常谈的bug给从根源上消除了——就是那种函数返回局部变量引用的情况。他举了这么一个典型的错误示例代码:

int& bad_bunny() {
    int local = 42;
    return local;
}

用GCC编译器编译这段代码的话,会直接抛出明确的错误提示:

error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'

备注:内容来源于stack exchange,提问作者Zebrafish

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.13 16:10:28