I have a const char and a const wchar_t. My function below works with the char. What\'s the simplest/most efficient way to write a function that can easily handle both char an
You can use template for this, like below,
template
const CharT* replaceSubstring(const CharT* find, const CharT* str, const CharT* replace);
template<> const char* replaceSubstring(const char* find, const char* str, const char* replace)
{
std::string const text(str);
std::regex const reg(find);
std::string swap_str(replace);
return std::regex_replace(text, reg, swap_str).c_str();
}
template<> const wchar_t* replaceSubstring(const wchar_t* find, const wchar_t* str, const wchar_t* replace)
{
std::wstring const text(str);
std::wregex const reg(find);
std::wstring swap_str(replace);
return std::regex_replace(text, reg, swap_str).c_str();
}
Also, overloading can be an other option.
You may get good advice from below link.
Is it possible to have a C++ method accept either const char* and const wchar_t* as a parameter without overloading the method?