On 05.07.17 11.01, peter koch wrote:
>> Well I think that strings of statically typed size are of little use
>> unless you intend to port very old code from Cobol or something similar.
>
> Why? I guess it very much depends on your application area.
> Personally, I have much use of strings with a static maximum size, using them whenever I program against an interface which defines maximum sizes for I/O.
A static *maximum* size is helpful, indeed.
But AFAICS the example always allocated the maximum size. This is big
waste of memory unless the limit is only a few bytes.
>> On the other side /immutable/ strings /are/ very useful, especially in
>> multi-threaded environments because they are intrinsically thread-safe
>> for read operations.
>>
>> Marcel
> Certainly! Immutable and hopefully also constexpr so that I can manipulate them while compiling.
Well, this is an even more advanced topic, since the restrictions for
constexpr are quite tight. I don't think that a string class can have a
/runtime/ dynamic length and constexpr (factory) functions at the same
time, at least not with C++11.
I had a similar requirement years ago. I.e. neither the string class
constructors of program constants should invoke dynamic allocations at
program startup, nor a dynamic allocation and string copying should
happen every time the constant is accessed. In fact no copying of the
constant data should be done at all.
I ended up with a distinct class for this purpose. This one had the
string length in the type and used the internal data structures of the
'normal' string class in a way that prevented the deallocator from
running so it could bind to static storage rather than heap storage.
It should be possible to write a class for your requirements that is
compatible to the matching immutable string class.
Marcel