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

标签 c++ fluent-interface

我刚刚读过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;
};

也可以使用“流畅接口(interface)”方法编写:

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/

相关文章:

c++ - c tcp套接字非阻塞接收超时

c++ - 如何在 MFC 中向 CMenu 添加子菜单?

c++ - 使用 C++ 将子类与父类链接的方法

带有队列或列​​表的 C# 流畅接口(interface)

oop - 流畅的接口(interface)和有漏洞的抽象

c# - 如何简化同一开放通用实现的大量封闭通用版本的注册?

c++ - 在 C++ Windows 应用程序中嵌入和扩展的简单解释器

c++ - Makefile 在 Linux 下工作但在 Windows 下不工作,无法在子目录中找到文件

c++ - 字符串数据类型如何存储为字节

php - 方法链的影响