Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline template function?

Do I need inline template functions if they are included in several cpp files? Thanks.

template<bool> inline QString GetText(); template<> inline QString GetText<true>() {return "true";} template<> inline QString GetText<false>() {return "false";} 
like image 561
user1899020 Avatar asked Jul 16 '13 02:07

user1899020


People also ask

Can you inline a template function?

Yes, you need the inline specifier there. The ODR (one-definition rule) states that there must be exactly one definition of a variable, function, class, enum or template.

What is the function of inline?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Do templates need to be inline?

So in summary: For non fully specialized function templates, i.e. ones that carry at least one unknown type, you can omit inline , and not receive errors, but still they are not inline . For full specializations, i.e. ones that use only known types, you cannot omit it.

What is inline function and example?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.


2 Answers

You do, because those are full function specializations, and therefore subject to the one-definition rule just like normal functions.

like image 198
user541686 Avatar answered Sep 22 '22 21:09

user541686


Yes, you need the inline specifier there.

The ODR (one-definition rule) states that there must be exactly one definition of a variable, function, class, enum or template. Exceptions relevant for your question are listed in §3.2/5 (C++11) (emphasis mine):

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. [...]

Template specializations for which all parameters are specified (i.e. explicit specializations) are not listed there, and §14.7.3/12 says:

An explicit specialization of a function template is inline only if it is declared with the inline specifier or defined as deleted, and independently of whether its function template is inline. [ Example:

template<class T> void f(T) { /∗ ... ∗/ } template<class T> inline T g(T) { /∗ ... ∗/ } template<> inline void f<>(int) { /∗ ... ∗/ }  // OK: inline template<> int g<>(int) { /∗ ... ∗/ }          // OK: not inline 

— end example ]

like image 33
jogojapan Avatar answered Sep 20 '22 21:09

jogojapan