
Message du 09/05/11 16:22 De : "Stewart, Robert" A : "'boost@lists.boost.org'" Copie à : Objet : Re: [boost] [review] string convert
Vicente BOTET wrote:
De : "Vladimir Batov"
From: "Stewart, Robert" ...
int i = boost::convert::to(str);
That's good.
How about we simplify the API down to just:
T convert::to(S, boost::parameter-list);
As Robert said, the name of a free function must be readable when introducing a using namespace. Or is convert a class?
I think to("FF") is readable.
Maybe, but the name doesn't scale. So I will prefer to preserve the convert_to prefix.
Then we specialize it for convert::result type so that
convert::result convert::to>(S, boost::parameter-list);
I don't see any inconvenient in introducing a special overload.
I presume you mean that such an overload could be convenient for someone like Vladimir and would not cause a problem for those preferring other approaches.
Right.
I guess a user needs to call it as follows
convert::result r = convert::to>(S, boost::parameter-list);
It's hard to say as that was munged.
convert::result r = convert::to(*convert::result*)(S, boost::parameter-list);
// The predictable. Throws on failure int i = convert::to("FF", (format_ = std::hex))
Would this overload exist only when the source parameter is string? Sorry, but I don't see how it could be used for type- to-type conversions
If it supports type-to-type conversion via an I/O stream, it would apply, wouldn't it?
It would, but I will prefer to use the stream syntax for that kind of conversion instead of using additional parameters.
int i = convert::to(aValue, (format_ = std::hex))
Would the std::hex be applied to the output or the input stream?
ios << aValue; ios >> std::hex >> i;
or
ios << std::hex << aValue; ios >> i;
Are these equivalent?
I think the interface must state clearly when these manipulators are applied.
I think it should be something like this:
convert::as(aValue, (in_ = std::hex, out_ = std::hex));
Hmmm, I don't tisk this is enough readable.
Vladimir, as I said you my library doesn't pretend to take care of string conversions or conversions via a stream.
I presume you mean string-to-string conversions when you write "string conversions" ...
No, I meant conversion from string to a type and from a type to a string. As I tried to explain in this and other posts, to take care of a type-to-type conversion via a streams you needs to state which manipulators are applied to the ostream and which one to the istream. Your in_, out_ is first trial but must be generalized to several manipulators.
My library will specialize the convert_to function when one of the parameters is a string and call the best library providing it.
...because of that.
The intent of Boost.Conversion is to support generic conversion that can be specialized for specific types. One of the major differences between Boost.Conversion and Boost.Convert, a part from the interface is the customization point. In the case of Boost.Convert it is the class convert and defaults conversion using a iostream. Boost.Conversion is customized by overloading the convert_to function, and by default uses the target conversion operator from a source. Best, Vicente