Normal function not overwriting template function
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.
开发者_StackOverflowDoes anybody have a clue, where the problem could be?
[1] http://www.gotw.ca/publications/mill17.htm
You're breaking the one-definition rule.
Unless a function is inline
, it can only be defined once. If you mark the function as inline
, so long as the definitions match they can be defined as often as desired. Template functions behave as if they were implicitly inline
, 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 as inline
:
template <>
inline void foo(std::string& value);
(If you're getting this before link time, you need include guards.)
精彩评论