samedi 2 avril 2022

Convert a group of C++ functions into C++ Template

I have a group of functions which perform similar action on different input parameter classes ParamX, ParamY, ParamZ. They have a general form like:

Result funcX(ParamX param){
  
  auto result = param.getX();
  
  // check result is valid 
  if(!result.valid()){
     // print a log message for ParamX
     return {};
  }
  
  return result.value();
}

Result is a templated struct like Result<ParamX>, based on the type of input parameter class ParamX.

So given I have several different functions funcX, funcY, funcZ, each acting on ParamX, ParamY, ParamZ separately, how do I convert this into a single template function?

Aucun commentaire:

Enregistrer un commentaire