Say i have a class like this
template<typename CharT>
class basic_class
{
public:
using char_type = CharT;
using string_type = std::basic_string<CharT>;
private:
const char_type ch = '?';
const string_type str{"How to init"};
};
It is okay if it is char but not for wchar_t.
- How can i do this ?
EDIT:
I decided to write a function that converts form char and string to various type,like
template<typename To>
constexpr To to_type(char val)
{}
template<>
constexpr char to_type<char>(char val)
{
return val;
}
template<>
constexpr wchar_t to_type<wchar_t>(char val)
{
//....
}
template<>
constexpr char16_t to_type<char16_t>(char val)
{
//....
}
template<>
constexpr char32_t to_type<char32_t>(char val)
{
//....
}
and
template<typename To>
constexpr To to_type(std::string val)
{}
template<>
constexpr std::string to_type<std::string>(std::string val)
{
return val;
}
template<>
constexpr std::wstring to_type<std::wstring>(std::string val)
{
//....
}
template<>
constexpr std::u16string to_type<std::u16string>(std::string val)
{
//....
}
template<>
constexpr std::u32string to_type<std::u32string>(std::string val)
{
//....
}
Then i will use this like
template<typename CharT>
class basic_class
{
public:
using char_type = CharT;
using string_type = std::basic_string<CharT>;
private:
const char_type ch = to_type<char_type>( '?' );
const string_type str{ to_type<string_type>( "How to init" ) };
};
In if statements like
if ( ch == to_type<char_type>('?') )
{
//....
}
if ( str == to_type<string_type>("How to init") )
{
//....
}
- How to convert char and std::string other types ?
All suggestions are welcome, Thanks in advance.