I am encountering a problem very similar to the topic addressed here: How to properly specialize a templatized static const member that is the same type as its parent
Here's my code:
template <class T>
class p3d
{
public:
p3d() {
x = 0;
}
p3d(const T &_x) {
x = _x;
}
static const p3d<T> origin;
T x;
};
typedef p3d<float> p3df;
int main(int, char**)
{
p3df p = p3df::origin;
return 0;
}
// elsewhere in a *.cpp file
template<> const p3df p3df::origin(0.0f);
When compiled under a strict compiler like Clang, I get the following error:
explicit specialization of 'origin' after instantiation
template<> const p3df p3df::origin(0.0f); -> implicit instantiation first required here
The solution is to move
template<> const p3df p3df::origin(0.0f);
to be before the typedef. The problem with this solution is that it results in multiply-defined symbols, because now p3df::origin
is defined in every file that uses the vector header. Is there any way to properly specialize the static const typedef-ed templates, while avoiding duplicate definitions in all source files using the typedef?