gpt4 book ai didi

c++ - 定义流畅的界面是否会对性能产生影响?

转载 作者:搜寻专家 更新时间:2023-10-31 01:20:23 27 4
gpt4 key购买 nike

我刚刚读了this question ,这为我提出了另一个问题:

考虑这个类:

class Foo
{
public:
void setA(int a) { m_a = a; }
void setB(int b) { m_b = b; }
private:
int m_a, m_b;
};

也可以使用“流畅的界面”方法编写:

class Foo
{
public:
Foo& setA(int a) { m_a = a; return *this; }
Foo& setB(int b) { m_b = b; return *this; }
private:
int m_a, m_b;
};

现在,如果我编写以下代码片段:

int main()
{
Foo foo;
foo.setA(1);
foo.setB(2);
}

如果我使用该类的第二个实现,额外的 return 指令是否会导致性能差异?

我应该打扰吗? (我的猜测是“否”)

最佳答案

Is there a performance difference induced by the additional return directives if I use the second implementation of the class?

我不知道,你的编译器和优化器设置有没有?在您给出的确切情况下,我看不到任何可以阻止任何开销被优化的东西,但我可以想象为一个没有优化这种情况的深奥平台编写一个天真的、次优的编译器。

如果您认为这在特定情况下很重要,test it instead of assuming .

关于c++ - 定义流畅的界面是否会对性能产生影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4899992/

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