22. Template specialization


文章作者: cyclezone
版权声明: 本文为博主「cyclezone」的原创文章,采用 CC BY 4.0 许可协议。转载请附上原文出处链接和本声明 cyclezone !

22. Template specialization

  1. we use template function which has the original operation
    to cover some or several cases;

    but when template function crash some special case,
    the original operation cannot cover ,

    we may specialize the template function with the speicial operation ;

  2. in common ,function name is same in different cases,

    but with different parameter type and different operation;

template<typename T> bool i_FromString(T& t,std::string str)
{
  istringstream is(str);
  is.imbue(LOC_CHS);
  is >> t;
  return true;
}
template<size_t N> inline bool i_FromString(char (&sz)[N],std::string str)
{
  if (N>0)
  {
    memset(sz,0,sizeof(char)*N);
    strncpy(sz,str.c_str(),N-1);
    return true;
  }
  return false;
}
template<> inline bool i_FromString(char*& t,std::string str)
{
  const char* pChVal = str.c_str();
  lstrcpyn(t,pChVal,strlen(pChVal)+1);
  return true;
}

template<> inline bool i_FromString(float& t,std::string str)
{
  if(str != "")
  {
    float fVal = atof(str.c_str());
    memcpy(&t,&fVal,sizeof(float));
    return true;
  }
  return false;
}

template<> inline bool i_FromString(double& t,std::string str)
{
  if(str != "")
  {
#if defined(BUTTER_ANDROID)
    double dVal = atof(str.c_str());
    memcpy(&t,&dVal,sizeof(double));
#else
    t = atof(str.c_str());
#endif
    return true;
  }
  return false;
}

template<> inline bool i_FromString(GUID& t,std::string str)
{
  CString   strTemp;
  if(str != "")
  {
    strTemp = str.c_str();
    CvtStringToGuid(strTemp,t);
    return true;
  }
  return false;
}

template<> inline bool i_FromString(string& t,std::string str)
{
  t = str;
  return true;
}
template<> inline bool i_FromString(CString& t, std::string str)
{
  t = str.c_str();
  return true;
}
文章作者: cyclezone
版权声明: 本文为博主「cyclezone」的原创文章,采用 CC BY 4.0 许可协议。转载请附上原文出处链接和本声明 cyclezone !

评论