4

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 and wchar_t?

    const char* asciiChar = "this is an ascii string";
    const wchar_t* unicodeChar = L"this is a unicode string";

    std::string replaceSubstring(const char* find, const char* asciiChar, const char* replace)
    {
        std::string const text(str);
        std::regex const reg(find);
        std::string const newStr = std::regex_replace(text, reg, replace);
        return newStr;
    }
fxfuture
  • 1,910
  • 3
  • 26
  • 40

3 Answers3

9

For this reason exactly, regex is a typedef of basic_regex<char>, much like string is a typedef of basic_string<char>. Knowing this, you can get away with a single template:

template<typename CharType>
std::basic_string<CharType>
  replaceSubstring(const CharType* find, const CharType* str, const CharType* rep)
{
    std::basic_string<CharType> text(str);
    std::basic_regex<CharType> 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<CharType>& parameters instead, too.

zneak
  • 134,922
  • 42
  • 253
  • 328
  • Thank you. This is perfect. I'm also trying to do the same with another function if you can help! Thank you! http://stackoverflow.com/questions/23644729/c-count-matches-regex-function-that-works-with-both-char-and-wchar-t – fxfuture May 14 '14 at 02:34
3

you can use another function to overload the function:

    std::wstring replaceSubstring(const wchar_t* find, const wchar_t* asciiChar, const wchar_t* replace)
    {
        std::wstring const text(str);
        std::wregex const reg(find);
        std::wstring const newStr = std::wregex_replace(text, reg, replace);
        return newStr;
    }
hyun
  • 2,135
  • 2
  • 18
  • 20
2

You can use template for this, like below,

template<typename CharT>
const CharT* replaceSubstring(const CharT* find, const CharT* str, const CharT* replace);

template<> const char* replaceSubstring<char>(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<wchar_t>(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?

Community
  • 1
  • 1
hyun
  • 2,135
  • 2
  • 18
  • 20
  • Is there really a need to use explicit specializations? It looks to me like you could simply write the function generically to work with either char or wchar_t. `std::basic_string const text(str);` etc. – bames53 May 13 '14 at 05:56
  • Yes, You're right. I think @Brian's code is more useful and simple. – hyun May 13 '14 at 05:59