mardi 3 mai 2016

Function to return Different types based on input

I want to write one function which would return me String, Int, Float based on some criteria. Using C11. Tried couple of stuff already mentioned, doesn't work with template/auto. If the function has only one return then its fine moment i add another return say string, compilation error. error: invalid conversion from ‘int’ to ‘char*’

template<typename T>
T getProperty(int field,Json::Value root)
{
  for(int i =0;i < root["properties"].size(); i++)
  {
    if(root["properties"][i]["field"].asInt() == field)
    {
      if(strcmp(root["properties"][i]["type"].asString().c_str(),"int") == 0)
      {
      T convertedValue = (root["properties"][i]["currVal"].asInt());
        return  convertedValue;
      }


      if(strcmp(root["properties"][i]["type"].asString().c_str(),"string") == 0)
      {
      T convertedValue;
      sprintf(convertedValue,"%s",root["properties"][i]["currVal"].asString().c_str());
        return convertedValue;
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire