Do you really need the different literals, or can you use the iterator constructor?
const char *f = "Foo";
return std::basic_string<CharT, TraitsT>(f, f + 3);
Maybe with something a bit more robust than "3" in there, if you're worried about ease of changing the literal in future.
In response to the point that this isn't very nice, what about:
template <typename CharT, typename TraitsT, size_t N>
basic_string<CharT, TraitsT> proper_string(const char (&src)[N]) {
return basic_string<CharT, TraitsT>(src, src+N-1);
}
Then you have:
return proper_string<CharT, TraitsT>("Foo");
If you really need the different literal, then the only thing I've thought of so far is to create traits for it, which is really horrible:
template<typename T> struct FooString {
};
template<> struct FooString<char> {
static const char *value() { return "Foo"; }
};
template<> struct FooString<wchar_t> {
static const wchar_t *value() { return L"Foo"; }
};
... etc ...
return FooString<CharT>::value();