C++ template and inline
Asked Answered
S

3

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.

Saloop answered 12/9, 2010 at 12:52 Comment(4)
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.Ruthannruthanne
@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? :)Saloop
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.Regelate
@Benoît, it can be very interesting to know, especially if you are afraid of object code bloat.Pistareen
K
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.

Kiln answered 12/9, 2010 at 13:0 Comment(12)
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.Darsey
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.Cop
Charles, compilers are allowed to ignore inline qualifiers. parashift.com/c++-faq-lite/inline-functions.html#faq-9.1Aggie
According to [C++ templates The Complete Guide] (amazon.com/Templates-Complete-Guide-David-Vandevoorde/dp/…) you must declare the function as inlineMonger
@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.Introrse
@DeadMG I have a problem with this question that is answered with a negative votes. What's the conclusion so ?Ratable
@JuanBesa Could you give a chapter\paragraph number or a page number?Judejudea
@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)Monger
templated functions are not automatically inlineJerrilyn
@JuanBesa thank you very much, your comment learnt me something and it also solved a multiple definition error I was having while linking :)Amylene
Should template function then be implemented in abcI.hpp file or in abc.cpp file?Toothlike
@Toothlike In the header file.Horizon
U
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.

Utterance answered 12/9, 2010 at 14:35 Comment(1)
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?Judejudea
I
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.

Instructions answered 12/9, 2010 at 12:59 Comment(7)
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...Saloop
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.Instructions
That's not actually correct @Instructions - "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]Mellie
@Mellie - 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.Instructions
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.Mellie
@Mellie Does that mean for translation units that use the inlined functions they do not need to include the header file definition of the function?Flocky
@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]Mellie
K
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.

Kiln answered 12/9, 2010 at 13:0 Comment(12)
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.Darsey
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.Cop
Charles, compilers are allowed to ignore inline qualifiers. parashift.com/c++-faq-lite/inline-functions.html#faq-9.1Aggie
According to [C++ templates The Complete Guide] (amazon.com/Templates-Complete-Guide-David-Vandevoorde/dp/…) you must declare the function as inlineMonger
@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.Introrse
@DeadMG I have a problem with this question that is answered with a negative votes. What's the conclusion so ?Ratable
@JuanBesa Could you give a chapter\paragraph number or a page number?Judejudea
@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)Monger
templated functions are not automatically inlineJerrilyn
@JuanBesa thank you very much, your comment learnt me something and it also solved a multiple definition error I was having while linking :)Amylene
Should template function then be implemented in abcI.hpp file or in abc.cpp file?Toothlike
@Toothlike In the header file.Horizon

© 2022 - 2024 — McMap. All rights reserved.