Re: [boost] [Boost.Stringify] Yet another formatting library.
On Sat, Jun 8, 2019 at 9:53 AM Helmut Zeisel
Actually I did not check your library, but I would be interested in a formatting library that is not based on functions, but on function objects.
The usage would not be something like
std::string s1 = to_string("base10", 15); std::string s2 = to_string("base16", 42);
but
stringifier dec; dec.set_base(10); std::string s1=hex(15); stringifier hex; hex.set_base(16); std::string s2=hex(42);
To format values in Boost.Stringify, one does like this, for example: namespace strf = boost::stringify; int v = 255; std::string s = strf::to_string(v, " in hexadecimal is ", strf::hex(v)); assert(s == "255 in hexadecimal is ff"); So could maybe lambdas solve your need ? auto hex = [](int v){return strf::to_string(strf::hex(v)); }; auto dec = [](int v){return strf::to_string(v); }; auto s1 = hex(15); auto s2 = dec(42); assert(s1 == "f"); assert(s2 == "42");
With a function object based interface, one could easily store and reuse different formating options in parallel.
Not clear to me what you mean by that. Could you explain better ?
Von: "Roberto Hinz via Boost"
With a function object based interface, one could easily store and reuse different formating options in parallel.
Not clear to me what you mean by that. Could you explain better ?
This is a general difference between functions and funcion objects. If you use functions, you either need a global state (which is not thread-safe and has to be reset after every change because you might rely on the original state at some other place in the program), or you have to specify all options at every call (which might be quite a lot in the case of formatting). Function objects can store the state/options internally. When you use two (or more) different options alternating, you create two (or more) function objects and use these objects alternating without changing the global state and without specifying the different options again. Helmut
On Sun, Jun 9, 2019 at 2:40 AM Helmut Zeisel
Von: "Roberto Hinz via Boost"
With a function object based interface, one could easily store and reuse different formating options in parallel.
Not clear to me what you mean by that. Could you explain better ?
This is a general difference between functions and funcion objects.
If you use functions, you either need a global state (which is not thread-safe and has to be reset after every change because you might rely on the original state at some other place in the program), or you have to specify all options at every call (which might be quite a lot in the case of formatting).
Function objects can store the state/options internally. When you use two (or more) different options alternating, you create two (or more) function objects and use these objects alternating without changing the global state and without specifying the different options again.
Helmut
I see. Although Boost.Stringify does use function objects, they don't store formatting, but only things like numeric punctuation. So you need to specify all formatting at every call.
participants (2)
-
Helmut Zeisel
-
Roberto Hinz