gpt4 book ai didi

c++ - 禁用 "temporary to non-const reference"错误的 G++ 标志

转载 作者:太空狗 更新时间:2023-10-29 23:47:17 25 4
gpt4 key购买 nike

我正在寻找一些命令行标志(如果它们存在的话)来禁用此类 C++ 代码的 GCC 错误:

#include <string>

void m(std::string& s) { }

int main()
{
m(std::string(""));
}

G++ 给出了这个错误:

error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'

原因是能够从 VC++ 和 Sun Studio 快速迁移(无需任何代码更改),因为它们都默默地接受临时到非常量左值引用的转换。我知道在代码更改方面需要做些什么——我严格来说是在询问一种无需更改代码即可完成的方法。

我将使用 GCC 4.x。

最佳答案

为什么要禁用错误?修复代码而不是依赖供应商扩展。

关于c++ - 禁用 "temporary to non-const reference"错误的 G++ 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6371989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com