Gavin Lambert wrote
On 26/05/2014 02:59, quoth Thijs (M.A.) van den Berg:
The value of convert to me seems to be more in the context of generic programming for a wide range of types with a uniform interface than for a simple interface for specific types for which there are already simple & standard alternatives.
This comment in the context of strings made me think of stateful converters. I haven't really looked at the proposed library too closely, but is it supported/expected to have mutable converters?
An example of this in another conversion library was one that provided for conversion from std::wstring to const char *. This was handled by having a local instance of a "context" object (possibly analogous to the converter) that internally stored any memory allocated during the course of the conversion, such that the returned pointer remained valid as long as the context object was in scope, and no memory was leaked.
Furthermore, the way the templates were defined made it a compile error to try to perform that type of conversion without supplying a context object (whereas converting from std::wstring to std::string could be done with or without one, as it did not require intermediate storage).
The same context object could be used in multiple conversions and each would be kept distinct (so no conflicts between returned pointers), but all freed at the end.
Gavin, my humble apologies for answering your post/question that late.
Somehow I managed to miss it in the avalanche. Now that I am re-reading the
threads, I've come across your post and it made me very excited. You are
describing IMO a quite interesting/tricky conversion case that the proposed
design (with external converters passed in) handles seemingly with ease. In
the proposed design converters are created/configured independently from
actual conversion. So, they can store configuration/state. Say,
std::sstream-based converter remembers hex/dec/oct/uppercase/locale
settings, etc. In the case of std::wstring -> "char const*" that might be as
follows:
char buf[...];
my_converter cnv;
optional