I have the following wstring:
std::wstring testVal("Test");
Which I need to place inside this value:
static const TCHAR* s_test_val;
So far I have tried:
static const TCHAR* s_test_val = (const wchar_t*) testVal.c_str();
static const TCHAR* s_test_val = (wchar_t*) testVal.c_str();
static const TCHAR* s_test_val = (TCHAR*) testVal.c_str();
static const TCHAR* s_test_val = (TCHAR*) testVal;
But without success; s_test_val keeps appearing as an empty string.
This one is correct on the condition that
UNICODEis defined in which caseTCHARwill be an alias ofwchar_t.If
UNICODEisn't defined, then you would need to perform some conversion, but it might not be worth the effort to support non-unicode builds. Your attempt that uses a cast would silently do the wrong thing in that case while this one safely produces an error.In case you don't care about non-unicode support (and I cannot think of a reason why anyone would), then I recommend minimising the use of
TCHARentirely.