143

While reading the documentation for boost::test, I came across the term "free function". What I understand is that a free function is any function that doesn't return anything (Its return type is void). But after reading further it seems that free functions also don't take any arguments. But I am not sure. These all are my assumptions. So could anybody define free function?

1 Answer 1

183

The term free function in C++ simply refers to non-member functions. Every function that is not a member function is a free function.

struct X {
    void f() {}               // not a free function
};
void g() {}                   // free function
int h(int, int) { return 1; } // also a free function
Sign up to request clarification or add additional context in comments.

3 Comments

let's say we have our main function in a different file and inside it we need to call a free function , so what should I do to have free functions in some other file that I will include it later in my main file ?? I mean should I make a hpp file where my free function are implemented there ( as static inline functions maybe ) ?
Usually you would declare them in a header file and implement them in a separate source file (with some exceptions like template functions). It would be better to open a new question on that specific topic though so people can answer you with more detail etc.
Also, all friend functions are considered free by the compiler. Even if defined within a class body. And another exception is that all lambdas or functors are NOT free. Functors are classes so they obviously are not free. But lambdas are functors underneath too, so they are not free either. And it doesn't matter if the lambda captures anything or if the functors stores state.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.