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?

3
  • 1
    inline is implied in the first block. I'm trying to find a reference to it in the standard. Commented Nov 7, 2019 at 21:48
  • I'm looking forward for such confirmation, that would answer everthing. Commented Nov 7, 2019 at 21:55
  • I'm not finding it. Perhaps someone with better knowledge of the standard can. Commented Nov 7, 2019 at 22:04

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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?
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.
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 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.
|
2

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.