c++ templates argument based compilation -
i want write template function calls different method based on type of template argument. ex-
template<typename t> void getdata(t& data) { if(t int) call_int_method(data); else if(t double) call_double_method(data); // call not compile if data int ... }
basically, getdata should calling 1 method based on type of t. other calls should not exist. possible somehow?
provide overloads, either of getdata
if need whole function have specific behaviour int
or double
:
void getdata(int& data) { call_int_method(data); } void getdata(double& data) { call_double_method(data); }
or call_x_methods
if portion of function's logic specialized:
template<typename t> void dotypespecificstuff(t& data) { /* stuff */ } void dotypespecificstuff(int& data) { .... } void dotypespecificstuff(double& data) { .... } template<typename t> void getdata(t& data) { dotypespecificstuff(data); // other stuff .... }
note overload resolution favour non-templates on function templates when types match, can sure right function called these 2 types.
Comments
Post a Comment