Re: multiple definition problems when using template specialization
Zhang Guangyu wrote:
On Apr 19, 2:59 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
Zhang Guangyu 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?
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?
Please start by reading section 35 of the FAQ.
Implementation of *real* functions cannot be in multiple translation
units unless the function is declared 'inline'. Implementations of
function *templates* aren't really code until the template is
instantiated, they *have* to be visible by the compiler in every
translation unit where they are used.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -
- Show quoted text -
The problem is that there are instiantiation in both B.cpp and C.cpp
so compiler will generate code for foo<int> in both units, but how the
linker does not generate an error of foo<int> redefinition?
Sorry, I don't understand the question. Didn't you in your original
post say that you did get an error if you defined the foo<int>
specialization? Without it the compiler is *in charge* of making sure
that the *compiler-generated* instantiation does not violate the ODR.
As soon as you defined (specialised) the function yourself, thus making
it *your* responsibility, the compiler washed its hands, and since you
got your definition in both units, you violated the ODR.
I think it's one of the instances of "compiler magic", basically the
same as making sure it's the same static object in all uses of an inline
function defined in a header, etc.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask