How to define a static constant variable of a template class

I am trying to create a vector class with predefined static constants for up, right, and forward, because they must be the same in all cases. How to determine this and is it possible?

I am trying to do something like this:

template <class T> class vec3 { public: vec3(T x = 0, T y = 0, T z = 0) : x(x), y(y), z(z) { } static const vec3<T> right; static const vec3<T> up; static const vec3<T> forward; T x, y, z; } 

castes:

 #include "vec3.h" template <typename T> const vec3<T>::right(1, 0, 0); template <typename T> const vec3<T>::up(0, 1, 0); template <typename T> const vec3<T>::forward(0, 0, 1); 

This results in a syntax error.

+2
c ++ const templates static-members
source share
2 answers

It should be (everything is in the header (you can use .inl or .hxx if you want to break the declaration from the definition)):

 template <class T> class vec3 { public: vec3(T x = 0, T y = 0, T z = 0) : x(x), y(y), z(z) { } static const vec3 right; static const vec3 up; static const vec3 forward; T x, y, z; }; template <typename T> const vec3<T> vec3<T>::right(1, 0, 0); template <typename T> const vec3<T> vec3<T>::up(0, 1, 0); template <typename T> const vec3<T> vec3<T>::forward(0, 0, 1); 

Demo

+6
source share
 template <class T> class vec3 { public: vec3(T x = 0, T y = 0, T z = 0) : x(x), y(y), z(z) { } static const vec3 right; static const vec3 up; static const vec3 forward; T x, y, z; }; template <typename T> const vec3<T> vec3<T>::right(1, 0, 0); template <typename T> const vec3<T> vec3<T>::up(0, 1, 0); template <typename T> const vec3<T> vec3<T>::forward(0, 0, 1); 
+1
source share

All Articles