template <class PointCoordType>
Rect{
public:
/* omitted */
void draw();
privite:
/* omitted */
};
void Rect<float>::draw(); // specializationd declaration
template <class PointCoordType>
void GRect<PointCoordType>::draw(){
/* blablabla */
}
[/source]
In Rect.cpp:
[source]
void Rect<float>::draw(){
/* function body here */
}
If I attempt to compile + link this along with some other compilation units that makes use of this class, an error is generated. The linker complains about the specialized function defined in Rect.cpp, saying it is already defined in some other compilation unit. I suppose this means that while compiling other modules, a Rect::draw() is already instanciated from the template definition, which means it is not making use of the specialized version of the function.
If I move the speicialized version into the header file, then the linker complains that the function is defined in multiple compilation units because the header file in included in several other files.
The only way I was able to stop the linker from throwing up is to move the templated function draw() definition into Rect.cpp. But although I did not test it, I'm fairly certain that this means only Rect::draw() will be available. Attempts to instanciate the class using other types will cause Rect::draw() to be undefined.
Does anybody know where to put the specilaized function def so that it coexists peacefully with the templated version?
Edited by - Odie on 11/20/00 5:28:17 PM
Edited by - Odie on 11/20/00 5:30:25 PM
Edited by - Odie on 11/20/00 5:31:59 PM
Edited by - Odie on 11/20/00 5:33:49 PM
template class member function specialization?
Hi all! I'm having some trouble specializing a class member function and having it link correctly. The template class looks like this:
In Rect.h:
I don''t think you can sepcialise a member function on a template paramter for the class, but you could do it if you add an extra function that wraps calls to the private ones. Something like this should do what you want:
template<class T>class SomeClass{ template<class TParam> void foo(TParam*) // dummy param needed to get round some MSVC bugs { cout << "generic" << endl; } template<> void foo<int>(int*) { cout << "int" << endl; }public: void bar() { foo(static_cast<T*>(0)); }};int main(){ SomeClass<float> testFloat; SomeClass<int> testInt; testFloat.bar(); testInt.bar(); return 0;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement