C++ template specialization for member function -


i'm trying implement basic vector3 (vec3) class. i'm struggling special case : vec3<size_t> addition vec3<int>.

how can make template specialization case?

any appreciated. ben

#include <array> #include <ostream> #include <vector>  // #define vec3f std::array<float, 3> // #define vec3u std::array<size_t, 3>  #define vec3f vec3<float> #define vec3u vec3<size_t> #define vec3i vec3<int>  template <typename t> class vec3 {     public:         vec3(): _a() {}         vec3(t x, t y, t z): _a({x, y, z}) {}         vec3(const vec3<t> & a): _a({a[0], a[1], a[2]}) {}          ~vec3() {}          /// print vec3.         friend std::ostream & operator<<(std::ostream & os, const vec3<t> & v)         {             os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")";             return os;         }          inline typename std::array<t, 3>::reference operator[](size_t i)         {             return _a[i];         }          inline typename std::array<t, 3>::const_reference operator[](size_t i) const         {             return _a[i];         }           /// test equality.         inline bool operator==(const vec3<t> & other) const         {             bool = abs(_a[0] - other._a[0]) < 1e-6;             bool b = abs(_a[1] - other._a[1]) < 1e-6;             bool c = abs(_a[2] - other._a[2]) < 1e-6;             return (a , b , c);         }          /// test non-equality.         inline bool operator!=(const vec3<t> & other) const         {             return not (*this == other);         }           /// vec3 same type addition.         inline vec3<t> operator+(const vec3<t> & other) const         {             return {_a[0] + other[0], _a[1] + other[1], _a[2] + other[2]};         }       protected:         std::array<t, 3> _a; }; 

your problem find common type between size_t , int template argument result. possible solution:

/// vec3 addition between vectors of different base type. template <class u> vec3<typename std::common_type<t, u>::type> operator+(const vec3<u> & other) const {     return{ _a[0] + other[0], _a[1] + other[1], _a[2] + other[2] }; } 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -