How can I combine everything I've written into one template without using overloads? [closed]

旧城冷巷雨未停 提交于 2021-01-28 21:56:50

问题


How to make one template so that it can be called with several parameters, without creating unnecessary overloads with code repetition

template <typename... T1>
std::filesystem::path createFullPath(T1&&... components)
{
  return (std::filesystem::path{components} / ...);
}

std::filesystem::path tipe_2_fix;
struct demo {
  template <class T1, class T2, class T3>
  demo(T1&& tipe_1, T2&& tipe_2, T3&& tipe_3) {
   if constexpr (std::is_same_v<T1, std::filesystem::path &>)
   if constexpr (std::is_same_v<T2, int64_t &>) {
     tipe_2_fix = std::to_string(tipe_2);
   } else {
     tipe_2_fix = tipe_2;
   }
   // code that is too lazy to repeat
   std::filesystem::create_directories(tipe_1 / tipe_2_fix);  
  }
  // I could do this, but I don't want to repeat a lot of code from the template, I showed this as an 
  // example of what I want to achieve  
  
  template <class T1,class T3>
  demo(T1&& tipe_1, T3&& tipe_3) {
   // code that is too lazy to repeat
   std::filesystem::create_directories(tipe_1);
  }
}

ptrdiff_t numer = 242;
int main () {
 // I need the template to work like this
 demo(tipe_1,tipe_2,tipe_3);
 demo(tipe_1,numer,tipe_3);
 demo(tipe_1,123,tipe_3);
 demo(tipe_1,tipe_3);

  // but i do it like this and it is very confusing
 demo(createFullPath(tipe_1,tipe_2),tipe_3);
 return 0;
}

I would like to combine it all into one template if possible

来源:https://stackoverflow.com/questions/65919803/how-can-i-combine-everything-ive-written-into-one-template-without-using-overlo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!