title: How to extract different datatype data from an ASCII array using templates
body: <p>I'm trying to extract different datatype data from an ASCII array using templates where each datatype data is separated by a delimiter(':' in this case) also each datatype data can be preceded and succeeded by whitespaces which needs to removed. But I'm getting template instantiation error. Can somebody enlighten me how to accomplish this task. This is my sample code:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;cstring&gt;
  
  
  class Extract {
  public:
      inline Extract() {
          length = 0;
          std::memset(recvBuff, '\0', sizeof(recvBuff));
      }
  
      inline ~Extract() {
      }
  
      int length;
      unsigned char recvBuff[1024];
  
      template&lt;typename T&gt;
      void extractData(short&amp; start, short&amp; end, T&amp; val, char delimiter, short dataType) {
          while(end &lt;= length) {
              if(recvBuff[end] == delimiter)
                  break;
  
              end++;
          }
  
          if(end &gt;= length)
              return;
  
          std::string token(recvBuff[start + 1], end - start - 1);
          std::string trimmedToken;
  
          std::string::size_type first = token.find_first_not_of(" \n\r\t\f\v");
          std::string::size_type last = token.find_last_not_of(" \n\r\t\f\v");
  
          if((first == std::string::npos) || (last == std::string::npos))
              return;
          else
              trimmedToken.assign(token, first, (last - first + 1));
  
          if(dataType == 1) { //char
              if(trimmedToken.size() == 1)
                  val = trimmedToken.at(0);
          }
          else if(dataType == 2) { //int
              if(trimmedToken.size() &gt; 0)
                  val = std::stoi(trimmedToken);
          }
          else if(dataType == 3) { //float
              if(trimmedToken.size() &gt; 0)
                  val = std::stod(trimmedToken);
          }
          else if(dataType == 4) { //std::string
              val.assign(trimmedToken);
          }
      }
  };
  
  
  int main(int argc, char* argv[]) {
       //"contains mixed ascii data of char, int, float separated by :" for example
       //" CMD : 123 : 3453.45646 : REP : T : 2424 : 3424 : 803424.2312 "
      char buff[1024];
  
      Extract objExtract;
      objExtract.length = 60; // some dummy value for time being
      std::memcpy(objExtract.recvBuff, buff, objExtract.length);
  
      short start = 0;
      short end = 1;
  
      std::string data1;
      objExtract.extractData(start, end, data1, ':', 4);
  
      int data2;
      objExtract.extractData(start, end, data2, ':', 2);
  
      double data3;
      objExtract.extractData(start, end, data3, ':', 3);
  
      std::string data4;
      objExtract.extractData(start, end, data4, ':', 4);
  
      char data5;
      objExtract.extractData(start, end, data5, ':', 1);
  
      int data6;
      objExtract.extractData(start, end, data6, ':', 2);
  
      int data7;
      objExtract.extractData(start, end, data7, ':', 2);
  
      double data8;
      objExtract.extractData(start, end, data8, ':', 3);
  
      std::cout &lt;&lt; data1 &lt;&lt; "\t" &lt;&lt; data2 &lt;&lt; std::endl;
  
      return 0;   
  }
  </code></pre>