
Ion GaztaƱaga <igaztanaga <at> gmail.com> writes:
Isn't that what the allocator argument to basic_string is for?
I agree with Dave, I don't see the need of a fixed string for IPC: a fixed_buffer_allocator for basic_string should do the job. You have two possibilities:
I also agree that it is a job for the allocator but I have tried and never got it to work properly. The new allocator proposal look promising but we are not there yet.
-> Use an allocator that has the buffer as member. The only drawback is that the string will create a temporary default constructed allocator and will assign it to the basic_string's member allocator. So you have a extra stack when constructing the basic_string. And the allocators have private, per-instance memory, and can't be swapped (like current Howard Hinnant's proposal says), so they are stateful allocators (which I think are not standard).
How do you set the size of the buffer. AFAIK the basic_string implementation is free to ask for any size memory as first allocation. e.g. gcc allocates extra space for internal data and also use some algorithm to optimize memory allocation. (If I remember correctly it will always round up to nearest multiple of 128 bytes except for small sizes.)
-> Use an allocator that holds the pointer and size of a buffer. This is more flexible but you can't freely pass the string because the lifetime of the buffer must be the same as the lifetime of the basic_string. But sometimes this could be very useful and you can swap the strings.
char buffer [100]; c_buffer_allocator a (buffer, 100); basic_string<char, std::char_traits<char>, a> str (a);
you probably mean: basic_string<char, std::char_traits<char>, c_buffer_allocator> str(a); Now explain how I use it with boost::filesystem or the string_algo library.