38

When I'm writing a simple (non-template) class, if the function implementation is provided "right in place", it's automatically treated as inline.

class A {
   void InlinedFunction() { int a = 0; }
   // ^^^^ the same as 'inline void InlinedFunction'
}

What about this rule when talking about template-based classes?

template <typename T> class B {
   void DontKnowFunction() { T a = 0; }
   // Will this function be treated as inline when the compiler
   // instantiates the template?
};

Also, how is the inline rule applied to non-nested template functions, like

template <typename T> void B::DontKnowFunction() { T a = 0; }

template <typename T> inline void B::DontKnowFunction() { T a = 0; }

What would happen in the first and in the second case here?

Thank you.

smerlin
  • 6,446
  • 3
  • 35
  • 58
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
  • May i ask why you want to know the answer to this question ? I've done quite a lot of c++ coding, and i haven't faced circumstances in which it mattered much. – Benoît Sep 12 '10 at 12:55
  • 20
    @Benoît The same here, but when writing some template-based chunk of code recently, I understood that I actually don't know what happens in the case which I've described. So why not ask a question? :) – Yippie-Ki-Yay Sep 12 '10 at 12:59
  • Not sure what your looking for the class question but I'm interested in the answer :o) I don't believe there's anything special going on with DontKnowFunction() and the inline. If the body is part of the class declaration then its considered implicit otherwise inline is used and the body is outside of the declaration. – skimobear Sep 12 '10 at 13:06
  • @Benoît, it can be very interesting to know, especially if you are afraid of object code bloat. – Johan Lundberg Jul 07 '12 at 08:15

3 Answers3

15

Since when you instantiate you get a class, that function is like an ordinary member function. It's defined in that class, so the function is automatically inline.

But it does not really matter here that much. You can define function templates or members of class templates multiple times in a program anyway - you don't need inline to tell the compiler about that like in the non-template case.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • What would happen if the inline is a qualifier for a function template, which is not a member of a class? Is inline implied in this way as well? – Anton Daneyko Mar 13 '13 at 14:20
1

The inline keyword is not a "rule". It is merely a suggestion/hint to the compiler and what it does with it is completely up to it and it's implementation. With this in mind, it's not possible to know what will happen with your examples. The compiler may in fact inline all, some, or none of them.

RC.
  • 27,409
  • 9
  • 73
  • 93
  • I know that `inline` keyword acts as a recommendation, but in the first example (when you write code right inside of the class definition) the `inline` is "automatically prepended", so it means that at least I give this recommendation to the compiler. But I don't know what would happen in template-based cases. I'm pretty sure there are some rules for that... – Yippie-Ki-Yay Sep 12 '10 at 13:03
  • Right, but whether the inline is there or not explicitly or implicitly, the compiler can do with it what it will thus you can't pre-determine the compilers behavior. – RC. Sep 12 '10 at 13:10
  • 1
    That's not actually correct @RC. - "An implementation is not required to perform this inline substitution [of the function body] at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected." [working draft 7.1.2/2] – boycy Apr 02 '13 at 13:56
  • @boycy - Can you elaborate? The essence of my answer is that you can specify inline all you want, but whether or not the compiler does it, is up to it and can it may choose to ignore your attempt to inline the function. It may also inline a function that isn't specified as inline such as when using templates. – RC. Apr 02 '13 at 17:52
  • 3
    Sure - the inline keyword performs 2 functions: one is to hint to the compiler hint that the function should be 'inlined' at the call site if possible, which is ignorable by the compiler. The 'other rules for inline functions' allow an inline function to be defined in every translation unit using it. If you link 2 compiled translation units, both having a definition of some non-inline function foo, a linker error 'multiple definition of foo' will be produced. – boycy Apr 03 '13 at 09:15
  • @boycy Does that mean for translation units that use the inlined functions they do not need to include the header file definition of the function? – Zachary Kraus Nov 19 '14 at 00:22
  • 1
    @ZacharyKraus no it doesn't. Even if the compiler won't inline the call to an `inline` function, you cannot call an `inline` function that has been declared but not defined. _"An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case."_ [N3337 7.1.2/4] – boycy Nov 20 '14 at 11:17
0

Templated functions as far as I know are automatically inline. However, the reality is that most modern compilers regularly ignore the inline qualifier. The compiler's optimizing heuristics will most likely do a far better job of choosing which functions to inline than a human programmer.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 23
    Compilers are _not_ allowed to ignore the `inline` qualifier and I don't know of any modern compilers that do. `inline` makes specific well-defined changes to the language rules for multiple definitions of functions. It's not just a hint. – CB Bailey Sep 12 '10 at 14:58
  • 24
    inline is a very specific and define meaning. But it has very little to do with "Code in-lining" and most modern compilers ignore it in regards to the `hint` it gives for "code in-lining". It terms of linking and other stuff it can not be ignored. – Martin York Sep 12 '10 at 19:02
  • 6
    Charles, compilers are allowed to ignore inline qualifiers. http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.1 – maayank Jun 09 '12 at 21:47
  • 3
    According to [C++ templates The Complete Guide] (http://www.amazon.com/Templates-Complete-Guide-David-Vandevoorde/dp/0201734842) you must declare the function as inline – Juan Besa Jul 17 '12 at 22:06
  • 1
    @LokiAstari +1 aye, I ran into this issue when including template functions from a lib from multiple layers - without the inline, I get multiply defined errors during linking. – kfmfe04 Dec 23 '12 at 16:27
  • 1
    @DeadMG I have a problem with this question that is answered with a negative votes. What's the conclusion so ? – Stephane Rolland Jan 31 '13 at 09:36
  • @JuanBesa Could you give a chapter\paragraph number or a page number? – Anton Daneyko Mar 13 '13 at 14:17
  • 7
    @mazhaka Sure: Section 6.4 page 72. "This may lead to the impression that function templates are inline by default. However, they're not. If you write function templates that should be handled as inline functions, you should use the inline specifier (unless the function is inline already because it is defined inside a class definition) – Juan Besa Apr 26 '13 at 15:24
  • templated functions are not automatically inline – chila Jun 17 '13 at 21:44
  • @JuanBesa thank you very much, your comment learnt me something and it also solved a multiple definition error I was having while linking :) – Avio Sep 17 '13 at 09:25
  • Should template function then be implemented in abcI.hpp file or in abc.cpp file? – Daniel Mar 07 '14 at 19:07
  • @Daniel In the header file. – L. F. Mar 28 '19 at 11:46