Z
Zhang Guangyu
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?
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?