4

Item 23 of Effective C++ (3rd edition) by Scott Meyers is titled: "Prefer non-member non-friend functions to member functions". I understood that the intention of Scott was that, whenever a function can be implemented in terms of the existing minimal set of member functions, it should be made free-standing.

However, I saw an opinion today that having a free-standing function that calls a state-changing member function is a bad practice. This opinion basically states that all free-standing functions should be pure. Is this a universal understanding or my initial understanding is justifiable as well?

  • Can you provide a example of the function calls? I can probably determine if its a best practice or not from that. – rozar Feb 17 '16 at 17:55
6

It's far from a universal understanding. In fact, a large part of the C++ standard template library (like <algorithm> for a prominent example) follows Scott Meyers' advice while mutating the underlying container.

I imagine the primary argument of the opinion you saw is that the free-standing function "hides" the state change of the underlying member function, but that's an argument against mutability in general, and would hold no matter what kind of calling function is in the layer above the state-changing method call. Don't get me wrong, I'm very much pro-immutability, but if you're going to use mutable objects, Scott Meyers' advice helps to keep it highly cohesive.

  • I am not sure I understand what you mean by "if you're going to use mutable objects, Scott Meyers' advice helps to keep it highly cohesive." What is cohesive in this context and how does Scott Meyers' advice help? – AlwaysLearning Feb 18 '16 at 8:11
  • 1
    Software cohesiveness is a measure of how well a module's code is aligned to the same purpose. Encouraging a smaller core set of methods gives a greater likelihood they are all closely aligned to their core purpose. – Karl Bielefeldt Feb 18 '16 at 13:52
1

a.fn(b) and fn(a,b) have exactly the same user semantic power. The difference is the capability of the first to access a's privates. (That's an A's developer problem, not really an A's user problem)

When generic programming is employed, free function can have SFINAE enabling and can have more flexible fine tuning between different types (and that is what Meyers is looking at)

When pure pointer-based OOP is employed, member call is usually preferred to mean "runtime object substitution at work". Especially for native Javanese.

But they are both convention due to history, not really changing that much. You can always turn one to the other with a cost of an inline call.

  • Do you have a different edition of Effective C++? I did not find any mention of generic programming and SFINAE in Item 23 in my copy of the book... If you are talking about an article/video, then could you please provide a link? – AlwaysLearning Feb 17 '16 at 18:19
  • The last edition is C++14 aware (and it is called "Effective modern C++, ISBN: 978-1-491-90399-5). What I'm alluding is std::enable_if related things. – Emilio Garavaglia Feb 17 '16 at 19:58
  • My question was based on Effective C++. – AlwaysLearning Feb 18 '16 at 8:12
  • I know, but if Mayers himself wrote another edition, may be it is because the one you have is not any more so ... effective! – Emilio Garavaglia Feb 18 '16 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.