CppCon 2017: Klaus Iglberger “Free Your Functions!”

CppCon · 40K views · 2017 · 1:01:42

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why non-member, non-friend functions beat member functions for encapsulation, testability and extensibility.

Recommended by 7 people across 7 different months, 2018 to 2023.

What it is

A CppCon 2017 conference talk by Klaus Iglberger of Siemens arguing that in C++ free functions should generally be preferred over member functions. He claims free functions deliver the coupling, cohesion, encapsulation, extensibility and testability that object-oriented design aims for.

What people said

Strong rec Fantastic Mr Fox · 2022-05 · Stack Overflow · on “Difference between friend function and getters”

There is an excellent talk about this subject here: CppCon 2017: Klaus Iglberger “Free Your Functions!” [this video] My take-away is that if a member function needs no access to the internals of a class, then it should not be a member function. In your case, how will Class A be usable? It is not right now, but I imagine you will have something like explicit operator int() const or int get_val() const . You can simply use this in a free function: A operator+(A lhs, B rhs) { return A{lhs.get() + rhs.get()}; } B operator+(B lhs, A rhs) { return B{lhs.get() + rhs.get()}; } int main() { ... cout << "sum: " << (objectA + objectB).get() << "\n"; ... Or something similar.

Recommends BobMorane · 2018-03 · Stack Overflow · on “Unit test private methods by making them free functions”

In the 2017 cppcon videos, I came across a talk by Klaus Iglberger which was entitled " Free Your Functions! [this video] ". In this talk, the speaker talked about how switching to free functions could easy up the process of testing private methods (See at 19:00). The idea is that you pull the private method out of the class (you make it a free function) and it becomes testable. At first, I found the idea interesting, but then the more I thought about it, the less I understood how this is actually supposed to work. For example, let's say I have the following (dummy) class: class SomeClass { public: SomeClass(); ~SomeClass(); void someTask(); private: void someComplexTask(); void someOtherComplexT

Recommends nyarlathotep108 · 2018-05 · Stack Overflow · on “The "free your functions" approach: could IDEs provide hints?”

I have been listening to Klaus Iglberger speech on CppCon 2017 called "Free Your Functions" (you can find it on Youtube here: [this video] ) which exposes better the rationale behind a concept coming from the Effective C++ book guideline: "Prefer non-member non-friend functions to member functions" . Looking at all the advantages and disavantages, there is one disavantage that is puzzling me very badly, which is: you do not call those functions using the operator. , and they are free to be defined anywhere. Reading this article about The Interface Principle , which talks extensively about what should to be considered a class public interface ( [LINK] ), its pretty much clear to me that those free

Recommends Julius · 2018-06 · Stack Overflow · on “Selectively instantiate member functions of a template with SFINAE”

You are asking for an elegant way to provide a functionality for some `T` only. Have you considered using a non-member function? In general, preferring free functions over dozens of member functions can also help to improve encapsulation etc. Some IMHO good arguments are presented in the talk Free Your Functions [this video] by Klaus Iglberger.

Recommends Justin · 2018-09 · Stack Overflow · on “normally should never public subclass vector<> but what if just convenient methods?”

@SwissFrank Actually not UB. It's perfectly well defined (excepting weird implementations; I'm unsure if `std::vector` is allowed to be `final` for instance). As for why, this CppCon 2017 talk [this video] explains it

Recommends t.niese · 2019-03 · Stack Overflow · on “C++: How can I use different implementation of methods with the same data class?”

All data type in the std library have only a minimum amount a member function, only those that are required hide the implementation of the data and to provide a public interface. Every thing else are free functions. And if done correctly then this is c++ and not c. You might want to take a look a the CppCon 2017: Klaus Iglberger “Free Your Functions!” [this video] talk.

Recommends dscerutti · 2023-08 · Stack Overflow · on “In C++, is a free function taking a struct as an argument faster than a class with a member function to do the same thing?”

I am alluding to Klaus Iglberger's timeless lecture [this video] here. I have a (relatively) simple function that takes a single float as input and returns the result as another float. The function needs a number of parameters, however, which have to be pre-computed (otherwise it's not fast at all). The way I see it, I can either make a struct to hold the pre-computations and then feed that to a free function along with the argument of the function to approximate, or write a class to hold the pre-computations as private members with a public member function to perform the approximate calculation based on the argument of interest. I'm not looking to delve into opinions, here. Either approach is ar

Cites Evg · 2018-10 · Stack Overflow · on “C++ matrix class operator using std::common_type_t and implementation of complex number”

You should not mix two templates into one. Try this: template<typename TT> template<typename T> Matrix<T> Matrix<TT>::operator+(const Matrix<T> &M) { using C = std::common_type_t<T, TT>; Matrix<C> temp(_rows, _cols); for (size_t i = 0; i < size(); ++i) temp.mtx[i] = mtx[i] + M.mtx[i]; return temp; } However, the return type is Matrix<T> and you return Matrix<C> . So I suggest you write it like this (don't forget to adjust the declaration accordingly): template<typename TT> template<typename T, typename C = std::common_type_t<T, TT>> // pseudocode, see below Matrix<C> Matrix<TT>::operator+(const Matrix<T> &M) const { Matrix<C> temp(...); ... return temp; } Complete example: template<class T>