C ++ implementation of friend / inline functions

I can not find the answer to this question for beginners. If I have a class // Header file (.h)

Class X { public: friend bool operator==(const X&, const X&); inline size_type rows() const; }; 

etc ... when I go to implement the .cpp file of X, I should include the words inline and friend in the function names in the .cpp file. those. I have to implement my file similar to below

 // CPP file (.cpp) #include "Xh" friend bool operator==(const X&, const X&) { //implementation goes here //return true/false } inline size_type rows() const { return r; } 

or I should not include them, for example, below

 #include "Xh" bool operator==(const X&, const X&) { ... } size_type rows() const { ... } 
+6
source share
3 answers

No, you should not, i.e. The second version is correct.

friend can only be used in a class definition (it doesn't matter outside of it), and the compiler searches for a function signature to determine the definition of a friend function declared in your class X

inline used in header files (although, I think, it is technically possible to use it in a cpp file as well, it just doesn't make sense there). Note that inline only makes sense if you are actually defining a function right there - it cannot have any effect if you provide the function definition separately in the cpp file.

The inline point is to hint to the compiler that the function in question can be built-in (this is not a guarantee, however - the compiler can decide whether it will be built-in to this function or not). If the function is built-in, any calls to it are replaced by a copy of the function body. This is almost always done to increase productivity in order to save the cost of function calls due to the potentially increasing program size. Now, if we want to embed a function, we want it to be built-in everywhere, not only within a single compilation unit; therefore, it makes no sense to use the inline inside the implementation file.

+6
source

When defining friend functions, you can use one of two options:

Define friend function inside class definition

 namespace Test { class test { int priv; friend void foo( test const & t ) { std::cout << t.priv << std::endl; } }; } 

Define them in the encompassing namespace:

 namespace Test { class test { int priv; friend void foo( test const & t ); }; void foo( test const & t ) { std::cout << t.priv << std::endl; } } 

But note that there are differences in the search. In particular, the first case is more restrictive (and therefore should be preferable), since it will find only ADL (Argument Dependent --aka Koening - Lookup), and in the second case, the function will be taken into account whenever this namespace is considered.

That is, for the compiler to consider the first function as an overload, the argument at the place of the call should be test , while in the second case it will be taken into account whenever the identifier matches and can be considered as a valid option if the argument is converted to test .

+3
source

You do not need a friend prefix in the function body, just a signature in the header.

0
source

Source: https://habr.com/ru/post/895587/


All Articles