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";}
There is no reason for inline for template declaration but not for template full specialization, you don't need to add the inline keyword for the first line but the second and third one need it. But each translation unit, which use the template, need to contains the template definition so the best way is to include it in header file and include in other cpps which use it.
In C++ standard n3376 for 3.2/6, there can be more than one definition of class template for the whole application, given the definition is same.
===============
Update the answere base on Jesse Good comments, (need inline for template full sepcialization) Thanks Jesse Good point out that.
You do, because those are full function specializations, and therefore subject to the one-definition rule just like normal functions.
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):
Template specializations for which all parameters are specified (i.e. explicit specializations) are not listed there, and §14.7.3/12 says:
It seem that the template method must be defined in the same file which is building, You don't need to use the 'inline' keyword for they were build in each cpp file which include it.