how to do an if else depending type of type in c++ template? -
// template specialization #include <iostream> using namespace std; // class template: template <class t> class mycontainer { t element; public: mycontainer (t arg) {element=arg;} t increase () { //if(t.type==int)//how or similar? //do if int return ++element; //if(t.type==char) //if ((element>='a')&&(element<='z')) //element+='a'-'a'; //return element; } };
i know how write template specialization , separate whole class def char type.
but if wanted handle in 1 block of code?
how can check if t int or char?
you use typeid
:
if (typeid(t) == typeid(int))
or use std::is_same
type trait:
if (std::is_same<t, int>::value)
Comments
Post a Comment