gpt4 book ai didi

c++ - NRVO 在抛出异常时有效吗?

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

我希望了解 C++ 中 NRVO 的局限性。具有初始命名声明和单个返回的函数是否会针对 NRVO 进行优化并省略 T val,即使函数 myFunc() 具有抛出的可能性?

T myFunc(bool toThrow) {
T val;

if (toThrow) {
throw std::exception();
}

// Do other things here.

return val;
}

最佳答案

我在 C++ Insights 上测试了您代码的几个版本和 Compiler Explorer .

简短的回答是肯定的,编译器很可能会使用 NRVO。

关于c++ - NRVO 在抛出异常时有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57121850/

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