Re: multiple definition problems when using template specialization
On 19 Apr., 20:57, Zhang Guangyu <linux...@gmail.com> wrote:
Using compiler vs2008.
I have declared a template function in foo.h.
foo.h:
template<class T> T foo(T value);
and defined it in foo.imp.
foo.imp:
template<class T> T foo (T value) { return value; }
I used this tempalte function in two files A.cpp and B.cpp which are
in the same project.
A.cpp:
#include "foo.h"
#include "foo.imp"
void funA() { foo<int>(1); }
B.cpp:
#include "foo.h"
#include "foo.imp"
void funB() { foo<int>(1); }
I would expect there is a link error of mulitple definition since both
the units have defined foo<int>, but the compiler worked fine. Why?
Your program (at least what I see) is well-defined and well-formed
because the one-definition rule (ODR) says so, see C++ 2003
standard (ISO/IEC 14882:2003(E)), 3.2 [basic.def.odr]/5:
"There can be more than one definition of [..] inline function
with external linkage [..], non-static function template (14.5.5),
[..] or template specialization for which some template parameters
are not specified in a program provided that each definition
appears in a different translation unit, and provided the definitions
satisfy the following requirements. [..]"
If you check the list of the requirements described there your
definition satisfies all these constraints.
Then I want to use explicit specialization for foo<int>, so I put a
specialized definition in the foo.imp.
foo.imp:
template<class T> T foo (T value) { return value; }
template<> int foo<int> (int value) { return value+1; }
Then the compiler gave a link error that foo<int> was redefined.
Is there a way to solve this problem?
The above quoted paragraph does not allow for multiple
definitions of non-inline non-template functions. To fix
your problem, make your specialization inline or move the
definition into a single translation unit (e.g. a separate .cpp).
As a reminder: There is no requirement that a compiler/linker
diagnoses violations of the ODR, see p.3 of above quoted
sub-clause:
"Every program shall contain exactly one definition of every
non-inline function or object that is used in that program;
no diagnostic required."
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]