1

I've read about inlining template function specalizations here

Apart from requiring inline keyword for instantation in a header which is logical, I can't find an answer whether it makes any difference to mark non specialized function templates as inline in a header? be it member functions or just normal functions.

Please save from explaining how compiler knows better than programmer, the question is: does inline keyword makes any sense for "unpacked" templates in a header?

are non specialized templates always inline since they are in a header? for bellow example pseudo code if we suppose Foo::bar method can be either very lengthy or very short, does omitting inline keyword or adding inline keyword has any effect on acctual chance of inlining or not inlining expanded function by the compiler?

Here is an example for member function templates which reflects my code:

class Foo
{
public:
    template<typename Type>
    static Type bar();
};

Example non inline definition:

template<typename Type>
Type Foo::bar()
{
    return Type();
}

Example inline definition:

template<typename Type>
inline Type Foo::bar()
{
    return Type();
}

Is this inline keyword above always useless, or it makes sense only if the function can be inlined by the compiler? are templates always inline in a header?

metablaster
  • 1,958
  • 12
  • 26

3 Answers3

2

The inline keyword is used to bypass the One Definition Rule, not for indicating inline substitution of the function call. Note that the compiler still needs the function definition to perform inline substitution, so you will have to provide inline if the definition of the function is in a header file that is included across different files.

A specialized function template is like an ordinary function because all the template parameters have been fixed (i.e., function templates can't be partially specialized). Therefore, if the (fully) specialized function template is in a header file, you should make it inline to cope with the One Definition Rule just in the same way you should do it with an ordinary function.

JFMR
  • 23,265
  • 4
  • 52
  • 76
  • thanks, I'm interested only about `inline substitution` case, does my example code make any difference with `inline` keyword on non specialized template functions for that case? for example I want to hint the compiler to inline the "expanded" template function in call site. – metablaster Nov 07 '19 at 21:43
  • I think you will have to look at compile-specific documentation for that. Anyway, if you want inline substitution of a function, keep in mind that **the compiler must have the definition of this function to be inlined at sight at the moment of the call to this function**. Otherwise, how can the compiler perform the inline substitution? – JFMR Nov 07 '19 at 21:48
  • That's the reason why you usually put the function definition in a header file and use the `inline` specifier: because this header may be included in multiple files so that the ODR can be broken. – JFMR Nov 07 '19 at 21:51
  • well if the template function is defined in a header then compiler does have a sight of definition right? I didn't mean to define templates in source file and put `inline` in a header. or not to include header where function is called and expanded. – metablaster Nov 07 '19 at 21:51
  • @metablaster yes, template functions are `inline` by default. Also `inline` is *automatically* added to a function defined inside the class definition. Therefore, it also applies to member functions of a class. – JFMR Nov 07 '19 at 21:52
  • yes I know, but my sample defines function outside the classs but inside the same header. in usual cases with normal functions that does not imply `inline`, can we say the same for template member functions, I don't know edit: ah OK, I see your point. so my 2 sample definition make no difference, both are inline? – metablaster Nov 07 '19 at 21:54
1

I think I found something about this, and it looks like inline does make a difference for template definition in a header, IE. if you omit inline then that function is not inline!

Reference link

Quote:

As an example, consider the header file Foo.h which contains the following template class. Note that method Foo::f() is inline and methods Foo::g() and Foo::h() are not.

// File "Foo.h"
template<typename T>
class Foo {
public:
  void f();
  void g();
  void h();
};
template<typename T>
inline
void Foo<T>::f()
{
  // ...
}

Looks like we can safely conclude that templates are not implicitly inline, but sample implies that other 2 methods are defined in cpp file, so all bets for conclusions are off. still looking for some standard confirmation.

metablaster
  • 1,958
  • 12
  • 26
0

It affects the behavior of explicit instantiation declarations (in a fashion meant to encourage actual inlining of inline function templates). In C++20’s modules, it also affects the ability to use internal-linkage (e.g., static) entities from such a template—again, in a way that’s meant to encourage inlining into importers. In the latter case, having the definition inside a class no longer implies inline.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76