I just read this question, which raises for me another question:
Consider this class:
class Foo
{
public:
void setA(int a) { m_a = a; }
void setB(int b) { m_b = b; }
private:
int m_a, m_b;
};
Which could also be written using the "fluent interface" method:
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;
};
Now, if I write the following code snippet:
int main()
{
Foo foo;
foo.setA(1);
foo.setB(2);
}
Is there a performance difference induced by the additional return
directives if I use the second implementation of the class ?
Should I bother ? (My guess is "no")
Since the methods are inlined, the compiler should be able to see that the return value is unused and possibly optimize it away.
Use whichever way makes the most sense for your project design. Then, measure the performance and only if it's not good enough would you consider optimizing the code.