metablaster
metablaster

Reputation: 2184

Effect of inline keyword on class member template functions?

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?

Upvotes: 1

Views: 1978

Answers (3)

Davis Herring
Davis Herring

Reputation: 39818

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.

Upvotes: 0

metablaster
metablaster

Reputation: 2184

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.

Upvotes: 2

jfMR
jfMR

Reputation: 24738

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.

Upvotes: 2

Related Questions