I would like to code a function that would look like:
template <typename CharT>
std::basic_string<CharT> convert(char const *);
and be used as follows:
convert<char>("Hello World!"); // "Hello World!"
convert<wchar_t>("Hello World!"); // L"Hello World!"
convert<char16_t>("Hello World!"); // u"Hello World!"
convert<char32_t>("Hello World!"); // U"Hello World!"
I could use std::codecvt
and co, but I find it almost pointless as it would be so easier to use some macro that adds L
, u
or U
with 0 cost.
Unfortunately templates and macros don't act at the same level... So here comes my question: would there be some way to mix them ? Or is there a better way ?
My main goal is to avoid repetition (specialization) of code: some functions I'm coding are CharT
templated and use string literals, and that would be the only difference. As an example:
template <typename CharT>
void foo (std::vector<std::basic_string<CharT>> const & vec,
std::basic_string<CharT> str = convert<CharT>("default"));
That would avoid to specialize foo() for each char type.
Many thanks for your help