Let's say we have the following files:
foo.h
namespace ns
{
template <typename T>
class Foo
{
public:
Foo();
~Foo();
void DoIt();
};
}
foo.cpp
#include "foo.h"
#include <iostream>
namespace ns
{
template <typename T>
Foo<T>::Foo() { std::cout << "Being constructed." << std::endl; }
template <typename T>
Foo<T>::~Foo() { std::cout << "Being destroyed." << std::endl; }
template <>
void Foo<int>::DoIt()
{
std::cout << "Int" << std::endl;
}
template <>
void Foo<double>::DoIt()
{
std::cout << "Double" << std::endl;
}
template class Foo<int>;
template class Foo<double>;
}
Is that the correct way to do explicit instantiation, assuming the type will only ever be used with int or double as type parameters? Or do you need to declare the explicit specialization in the header file as well?
Doing it the way I've shown works with visual studio, but a coworker has been having problems with GCC (Although I've just checked, and I think that's due to something else, but I'll post this question anyway)