I have following two functions:
void bar(const std::string &s)
{
someCFunctionU(s.c_str());
}
void bar(const std::wstring &s)
{
someCFunctionW(s.c_str());
}
Both of these call some C function which accepts const char *
or const wchar_t *
and have U
or W
suffixes respectively. I would like to create a template function to handle both of these cases. I tried following attempt:
template <typename T>
void foo(const std::basic_string<T> &s)
{
if constexpr (std::is_same_v<T, char>)
someCFunctionU(s.c_str());
else
someCFunctionW(s.c_str());
}
But this does not seem to work correctly. If I call:
foo("abc");
this will not compile. Why is that? why a compiler is not able to deduce the proper type T
to char
? Is it possible to create one function which would handle both std::string and std::wstring?
"abc"
isconst char*
and not a std::(w)string – Shushconst char[N]
, but it will decay to aconst char*
– Dorrenstd::initializer_list<char>
. – Creditschar*
. I should have been more specific. – Dorren