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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -