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
For this reason exactly, regex
is a typedef of basic_regex
, much like string
is a typedef of basic_string
. Knowing this, you can get away with a single template:
template
std::basic_string
replaceSubstring(const CharType* find, const CharType* str, const CharType* rep)
{
std::basic_string text(str);
std::basic_regex reg(find);
return std::regex_replace(text, reg, rep);
}
This correctly handles both char pointers and wchar_t pointers, and returns the correct type of string. You may want to accept const std::basic_string
parameters instead, too.