gpt4 book ai didi

c++ - VS 2010 中返回值的编译器优化

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:42 25 4
gpt4 key购买 nike

使用完全优化的 VS 2010/Ox 查看以下两个函数调用:

static string test1(const string& input)
{
return input;
}

static void test2(const string& input, string& output)
{
output = input;
}

如果我使用后一个 test2,那么函数总是被优化掉并且代码被内联。但是,除非我关闭异常,否则不会内联 test1。有人知道为什么吗?

此外,如果编译器使用返回值优化,我希望编译器能够在 test1 中像 test2 一样高效地工作,但它似乎没有这样做。这也是我的困惑。

我想使用第一个函数签名的原因是我有该函数的两个可编译版本。我想让调用代码始终调用 test1 并且当设置了某个编译标志时我希望它将输入附加到一个拷贝并返回它,当未设置编译标志时我希望它尽可能接近 no -op 尽可能。

最佳答案

Visual Studio 无法内联返回具有非平凡析构函数的对象的函数:

In some cases, the compiler will not inline a particular function for mechanical reasons. For example, the compiler will not inline:
  • A function if it would result in mixing both SEH and C++ EH.
  • Some functions with copy constructed objects passed by value when -GX/EHs/EHa is on.
  • Functions returning an unwindable object by value when -GX/EHs/EHa is on.
  • Functions with inline assembly when compiling without -Og/Ox/O1/O2.
  • Functions with a variable argument list.
  • A function with a try (C++ exception handling) statement.

http://msdn.microsoft.com/en-us/library/a98sb923.aspx

关于c++ - VS 2010 中返回值的编译器优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10067892/

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