c++ - Specialize template member function with already-deduced template parameter -


i want specialize member function of template struct based on type-traits of already-deduced struct template parameter. want 1 version of function when template parameter signed , when it's unsigned. not sure how go this.

the struct simple. represents size -- that's no doubt been written thousand times. here's simple version use types, guess:

template<class t> struct size {     t cx;     t cy;      // ... bunch of functions might expect.      // normalize function -- avoid negative sizes.  make sure size @ least 0, 0      void normalize()     {         cx = std::min(cx, 0);         cy = std::min(cy, 0);     } }; 

but of course processing function pointless unsigned types. i'd make no-op types.

for second, thought might try using enable_if along return type.

typename std::enable_if<std::is_signed<t>::value, size&>::type  normalize()  {      cx = std::max(cx, 0);      cy = std::max(cy, 0);      return *this;  } typename std::enable_if<std::is_unsigned<t>::value, size&>::type  normalize()  {     return *this;  } 

but won't work because (as understand it) because, @ point of member function, template 't' has been deduced , sfinae cannot used. please correct me if i'm wrong on that.

so guess write overloads true_type , false_type this:

void normalize(std::true_type) {     // ensure signed types @ least zero.      cx = std::max(cx, 0);      cy = std::max(cy, 0);  } void normalize(std::false_type) {     // nothing unsigned types } void normalize()  {     // call overload based on type.     normalize(std::is_signed<t>::type());  } 

but seem pointlessly construct std::integral_constant<bool>. offends sense of efficiency. if more complicated example, might affect performance.

so instead write member template function this:

template <typename t>  void normalizet()  {  }  template<>  void normalizet<std::true_type>() {     cx = std::max(cx, 0);      cy = std::max(cy, 0);  } void normalize() {     normalizet<std::is_signed<t>::type>(); } 

and i'm guessing there other approaches. sort of feel i'm missing obvious. take time boil stuff down simplest, clearest, robust version possible.

i realize it's not complicated "problem" here. , said, can make code work i'm looking more general lesson -- understand , recognize "pattern" involved because sort of situation find myself in often.

so given goals, there way write more cleanly or robustly?

do not optimize manually (with template). let compiler optimize on std::max (note: should use std::max "avoid negative sizes")

for clarification or if normalize more complex, might do:

void normalize() {     if(std::is_signed<t>::value) {         cx = std::max(cx, 0);         cy = std::max(cy, 0);         // ...     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -