Cannot figure out why I am getting the compiler errors with the following code:
#include <tchar.h>
typedef TCHAR Char;
typedef std::basic_string<Char> String;
template<typename C, typename T>
std::basic_string<C> InternalToString(T val);
template<typename T>
inline std::string InternalToString<char, T>(T val)
{
return std::to_string(val);
}
template<typename T>
inline std::wstring InternalToString<wchar_t, T>(T val)
{
return std::to_wstring(val);
}
template<typename T>
inline String ToString(T val)
{
return InternalToString<Char, T>(val);
}
if I compile the code with MSVC 2017 I get "error C2768: 'InternalToString': illegal use of explicit template arguments"
Partial specialization is not allowed for function templates, it could be used only for class templates.
You can apply function templates overloading with SFINAE. e.g.
// used when C is specified as char
template<typename C, typename T>
inline typename std::enable_if<std::is_same<C, char>::value, std::string>::type InternalToString(T val)
{
return std::to_string(val);
}
// used when C is specified as wchar_t
template<typename C, typename T>
inline typename std::enable_if<std::is_same<C, wchar_t>::value, std::wstring>::type InternalToString(T val)
{
return std::to_wstring(val);
}
// used when C is specified as other types
template<typename C, typename T>
typename std::enable_if<!std::is_same<C, char>::value && !std::is_same<C, wchar_t>::value, std::basic_string<C>>::type InternalToString(T val);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With