Is "inline" required with member function templates

If I have a member function template in a class without a template and you want to define it outside the class, do I need to use the "built-in"?

Example:

class A { template <class D> void someMethod(D param); } template <class D> /* inline needed here? */ void A::someMethod(D param) { } 

Section 3.2.5 of the standard states that function templates are not subject to the One Definition rule. Is a member function template a function template in this regard?

Edit: The linker doesn't complain without the built-in - but still - is it really C ++ 03?

Edit:

What I have learned so far: gcc (and presumably other compilers too) exports implicit template instances as weak characters, i.e. a conflict will not occur during the link if they are created in several translation units. Since weak characters are not part of the standard - does the standard somehow implicitly require template instances to act this way, and can I expect the same behavior with other standard compiler / linker combinations?

Since inline is mostly ignored for optimization, but allows many definitions of functions in different translation units, this implicitly translates these functions into export as weak characters. Does this template declaration mean the inline is redundant?

+4
source share
2 answers

inline as a keyword, to suggest to the compiler that a function marked as such are good candidates for optimization with the same name, and as help to implement such optimization, it requires that the function be defined - - in the same way - in each CU where it is used (it is much easier for the compiler to make optimization if he sees the code, even if linkers can perform the same optimization, this was much less common 20 years ago).

You might want to give this hint of function templates and thus mark the definition of the inline function template. If you do not want to give a hint, inline is not needed.

(Some time has passed when, for optimization, only functions marked as built-in were considered, but the keyword has not yet reached the fate of register , i.e. it is generally ignored by compilers, excluding its formal value, to prevent it from taking the address of a variable).

+1
source

Regardless of whether member function templates are function templates, they are, of course, not objects or non-built-in functions, so the definition rule does not apply.

But they are actually functional templates and are considered as such for other applications of the standard that are not specific to function templates that are not members.

0
source

Source: https://habr.com/ru/post/1437381/


All Articles