I have the following method used to convert an std::string object into an std::wstring one:
#include <string>
#include <type_traits>
#include <locale>
#include <codecvt>
template <class CharT>
inline std::basic_string<CharT> StringConverter( std::string input_str )
{
if constexpr( std::is_same_v <CharT, char> ) return input_str;
else if constexpr( std::is_same_v <CharT, wchar_t> )
{
static std::wstring_convert <std::codecvt_utf8_utf16 <wchar_t>> converter;
return converter.from_bytes( input_str );
}
return StringConverter<CharT>( "" );
}
This is used only to convert simple strings like:
#include <string>
#include <iostream>
using namespace std::literals::string_literals;
int main()
{
std::cout << StringConverter<wchar_t>( "Hello"s ); // returns L"Hello"
std::cout << StringConverter<char>( "Hello"s ); // returns "Hello"
}
and works well. However it seems a bit too much expensive in terms of performance: in the first case it needs 64 ns to convert the string, while in the second case 11.9. Do you know if there is a better way to write it or if it can be improved in C++17? Thanks.