There are many object of std::wstring created in my program as,
std::wstring mystr(L"test string");
As the input wchar string is string constant, it would save some heap usage if mystr is constructed as mystr.c_str() equals to the address of L”test string”. Is it possible to construct such wstring?
There are indeed valid use-cases for this but the C++ string class flat out doesn’t support it. If you really need it you are required to write your own string class which supports interning.
Note that several other languages support this natively; the C++ string classes unfortunately cover a rather specific use-case (lots of modifications) instead of the general one (lots of copies of essentially immutable strings).
To simplify the task, consider whether it’s enough for you to use a thin wrapper class around a pair of (const) iterators which could just point to the beginning and end of the
wchar_tbuffer in static memory.