c++ - Class and member function template specialization gone wrong -


i trying specialize color class , it's member function to() in order convert 1 colorspace , back. here's have far:

enum colorspace {     bgr, rgb, lab, hsv, xyz, yuv, hsl, cmy, yiq, luv, hlab, lch, ohta };  template<colorspace _cs, typename _dtp> class color;  template<typename _dtp> class color<rgb, _dtp> {  public:      color(_dtp r, _dtp g, _dtp b) : r(r), g(g), b(b) { ;; };     color() : color(0, 0, 0) { ;; };     ~color() { ;; };      _dtp r, g, b;      template<colorspace _cs, typename _dtp2 = _dtp>     color<_cs, _dtp2> to<>();      template<typename _dtp2 = _dtp>     color<hsv, _dtp2> to<hsv, _dtp2>() {         color<hsv, _dtp2> res;          rgb2hsv(r, g, b, res.h, res.s, res.v);          return res;     }  };  template<typename _dtp = double> class color<hsv, _dtp> {  public:      color(_dtp h, _dtp s, _dtp v) : h(h), s(s), v(v) { ;; };     color() : color(0, 0, 0) { ;; };     ~color() { ;; };      _dtp h, s, v;      template<colorspace _cs, typename _dtp2 = _dtp>     color<_cs, _dtp2> to<>();      template<typename _dtp2 = _dtp>     color<rgb, _dtp2> to<rgb, _dtp2>() {         color<rgb, _dtp2> res;          hsv2rgb(h, s, v, res.r, res.g, res.b);          return res;     }  }; 

each colorspace has each 1 class. each class has specialized functions to() every possible conversion. however, posting 2 of them obvious reasons. so, when try use conversion function:

color<rgb, double> c(255, 255, 125);  color<hsv, double> c2 = c.to<hsv, double>(); 

i following error:

error: class "color" has no member "to"

my last problem comes when try use default type class color:

template<typename _dtp = double> class color<rgb, _dtp> { ... ... 

in case can't write this:

color<rgb> c; 

it gives me error:

error: expected declaration

it might simpler add constructor conversion:

enum colorspace {     bgr, rgb, lab, hsv, xyz, yuv, hsl, cmy, yiq, luv, hlab, lch, ohta };  template <colorspace, typename = double> class color;  template <typename t> class color<rgb, t> { public:      color(t r, t g, t b) : r(r), g(g), b(b) {}     color() : color(0, 0, 0) {}     color(const color&rhs) = default;     ~color() = default;      t r, g, b;      template <typename t2>     /*explicit*/ color(const color<hsv, t2>& rhs)      {         hsv2rgb(rhs.h, rhs.s, rhs.v, r, g, b);     } }; template <typename t> class color<hsv, t> { public:     color(t h, t s, t v) : h(h), s(s), v(v) {}     color() : color(0, 0, 0) {}     color(const color&rhs) = default;     ~color() = default;      t h, s, v;      template <typename t2>     /*explicit*/ color(const color<rgb, t2>& rhs)      {         rgb2hsv(rhs.r, rhs.g, rhs.b, h, s, v);     }  }; 

with possible usage:

color<rgb, double> c(255, 255, 125);  color<hsv, double> c2{c}; 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -