I have to use an external library, but am getting a "multiple definition error" from following template function and its explicit specialization, if it gets called with a std::string.
template <typename T>
void foo(T& value);
template <>
void foo(std::string& value);
even if I change the 2nd function to
void foo(std::string& value);
the problem is the same.
According to [1] at least the version without a template (the "plain old function") should be prefered over the template version.
Does anybody have a clue, where the problem could be?
You're breaking the one-definition rule.
Unless a function is
inline
, it can only be defined once. If you mark the function asinline
, so long as the definitions match they can be defined as often as desired. Template functions behave as if they were implicitlyinline
, so you don't get errors with templates.However, an explicit specialization or non-template function is not implicitly
inline
and because you're including it in multiple translation units, you get multiple definitions; this breaks the rule. You should mark it asinline
:(If you're getting this before link time, you need include guards.)