Given the following test case
int main(int argc, char const* argv[]) {
{
constexpr wchar_t const* v = gstring<"hello world", wchar_t>();
std::wcout << v << std::endl;
std::wcout << std::wcslen(v) << std::endl;
}
{
constexpr char const* v = gstring<L"world hello", char>();
std::wcout << v << std::endl;
std::wcout << std::strlen(v) << std::endl;
}
return 0;
}
the output should be
hello world
11
world hello
11
This is useful when the character type is passed in as a type parameter. For example
template <typename TChar>
void AddFoo(std::basic_string<TChar> & s){
s.Append( gstring<"foo", TChar>() );
}
It requires a two stage process. The first step is to capture the
const char *using an implicit conversion to typeCapture. The second step is to call theAsmethod onCaptureto convert the buffer to the new type. All this is doneconstevaland via template parameters to ensure that the literal object is global.The gstring method can return the inner final buffer as
cast.string_becausecastis a template parameter and thus a compile time constant.See the solution on GodBolt https://godbolt.org/z/GaTnh69cM
Note: The conversion should really only be used from
char -> charorchar -> wchar_t. Obviously not all values inwcharhave values inchar