0

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?

Thomson
  • 20,586
  • 28
  • 90
  • 134
  • No, each string always has its own buffer. But why do you need multiple strings if they all have the same value? – Mr Lister Apr 12 '12 at 15:32
  • Possible duplicate of http://stackoverflow.com/questions/783944/how-do-i-allocate-a-stdstring-on-the-stack-using-glibcs-string-implementation – Eric Melski Apr 12 '12 at 15:33
  • Some compilers already skip the heap allocation for strings this short. [String class allocating on stack for small strings](http://stackoverflow.com/questions/5419016/string-class-allocating-on-stack-for-small-strings) – Bo Persson Apr 12 '12 at 16:25

2 Answers2

1

No. In order to do so, the implementation of std::basic_string would need additional information as to whether the wchar_t const* came from a string literal or not, and this information is simply not available. And even if it were, it would add to the cost of using the class, even when the argument wasn't a wchar_t const*.

If these strings are at global scope, the cost for constructing them is only at start up. And if they are local variables, you can always declare them static, so the cost of constructing them will only be paid once, and not every time you enter the block.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

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_t buffer in static memory.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Could you given example of such a use case? All I can think of is either a variable (and then you'll need only a single string, which you access from everywhere you need it) or a constant, and then you can simply use the literal! – Mr Lister Apr 12 '12 at 15:38
  • 1
    @MrLister The most obvious use-case is a parser which keeps in memory of identical copies of recurring identifiers and keywords. Another use-case comes from my domain (genome analysis) where you are dealing with lots of substrings of a genome (= several gigabytes) or lots (= billions!) of repetitive or identical small strings. But basically any application which uses recurring strings can profit from that (even if not a lot). Think ever single GUI with “OK” buttons. – Konrad Rudolph Apr 12 '12 at 15:42