4

I've been reading a c++ book, C++ Primer, and i was going through the class features and everything, and i encountered that, in a class most functions ( or every) are inline automatically.

What difference does it really make? explicitly defining an inline function vs implicitly defining an inline function, we could have already overloaded them inside the class scope anyways, i am finding it very difficult to understand this part. Is there any kind of performance gain by doing explicitly ?. in the photo we can see get() function using both methods explicit & implicit, can someone clarify me here.

This is the code i m talking about

I have to edit the question, because I've been told so many times that class members functions are not automatically inline. But the book c++ primer and other internet sources say that they are automatically inline.

Here is a piece of text from the book.C++ Primer clearly says that class functions are automatically inline..

Lalit Kumar
  • 134
  • 1
  • 9
  • @Dai how can i show it to you...ive read it in the book , that after C++11 , all member functions defined inside class are automatically inline. – Lalit Kumar Jun 20 '21 at 13:46
  • @Dai , maybe i misunderstood, but i will attach the photo [link](https://ibb.co/VJS5Yrx) – Lalit Kumar Jun 20 '21 at 13:48
  • 2
    @Dai the `inline` keyword is still required in c++. But it is not about telling the compiler that a function should be inlined, but for the linker to tell that multiple definitions of the same function are allowed in multiple compilation units. Member functions and function templates are implicitly inline. – t.niese Jun 20 '21 at 13:49
  • @t.niese Ah yes - I forgot about that use... – Dai Jun 20 '21 at 13:49
  • @Dai yes there are two main uses of inline i get it, but why use explicit vs implicit in an class ? does it make any difference?.. i am really sorry, i am confused – Lalit Kumar Jun 20 '21 at 13:52
  • @Dai Bro, i dont know why my question have been called "duplicate" while giving me link to somewhere that answers what is 'inline' , i think my question is quite different from just asking what is inline. – Lalit Kumar Jun 20 '21 at 14:24

1 Answers1

2

If a definition of a member function is within the class body it is implicitly inline. If you only declare it in the class body and you place the definition outside of it you need to make it explicitly inline.

This can be done in two ways:

struct test {
   inline void foo();
};

void test::foo() {
}

Or

struct test {
   void foo();
};

inline void test::foo() {
}

While both work, the second option is generally recommended.

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • thanks this made things somewhat clearer, it is just syntax arrangement thing then. i was thinking it had to do something with in preprocessing stage – Lalit Kumar Jun 20 '21 at 14:18
  • @LalitKumar `it is just syntax arrangement thing then` yes. If you don't want to have the declaration of the member function in the class body because you think that it is more readable, but you want to keep it in the header instead of moving it to a source file, then the specification requires that the function is inline, if the header is used by multiple compilation units. And if it isn't implicitly inline you need to explicitly add the `inline` keyword (see the linked duplicate for more details). – t.niese Jun 20 '21 at 14:26
  • i think i will accept this answer then, cool thanks, but i dont know why my question is marked as duplicate while it is no way duplicate, and it says the answer is already available [link](https://stackoverflow.com/questions/1759300/when-should-i-write-the-keyword-inline-for-a-function-method) . whereas my question is far away from just asking what is inline i never asked what is inline. – Lalit Kumar Jun 20 '21 at 14:27
  • @LalitKumar It is kind of a duplicate. `Is there any kind of performance gain by doing explicitly ?` is answered in the linked duplicate. `Why use explicit inline when every class member function is implicitly inlined` is a false assumption. Only member function declarations within the body are implicitly inline. The linked question says that you need `inline` if the function isn't implicitly inline. So this answer gives you the hint that you read the specification wrong and shows you the two ways of making an out of body function declaration inline. – t.niese Jun 20 '21 at 14:33
  • Repeating all of the details about inline does not make sense, so having an answer and closing the question as duplicate is IMHO fine. – t.niese Jun 20 '21 at 14:35
  • Sure, i think i might spend a little bit more time researching for my next question i guess, thanks though. – Lalit Kumar Jun 20 '21 at 14:38
  • @LalitKumar the question is fine, otherwise, you would have got downvotes ;) And the way the question is asked is different from the linked duplicate, so others that have the same problem in understanding the question, might find yours, and due to the duplicate find the other relevant information. – t.niese Jun 20 '21 at 14:38
  • @LalitKumar having a question marked as duplicate is not always bad. It often just means that it can be answered in short by a comment or answer. But it makes sense to link it to other questions so that the other details do not need to be repeated. – t.niese Jun 20 '21 at 14:42