|
Boost : |
Subject: Re: [boost] Formal Review Request: Boost.String.Convert
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-02-18 17:12:58
On Wed, Feb 18, 2009 at 2:05 PM, Andrey Semashev
<andrey.semashev_at_[hidden]> wrote:
> Emil Dotchevski wrote:
>
>> You can still do this with what I am proposing. When someone does
>>
>> foo x;
>> std::string s=to_string(x);
>>
>> this would bind to the foo to_string overload if available, if not it
>> would bind to the foo/ostream overload of << if available, etc. The
>> idea is to automatically pick the best conversion available to produce
>> the string the caller needs.
>>
>> This makes the to_string library implementation more complex, but the
>> benefit is that it is possible to define simple, non-template
>> to_string overloads for various types. The reason I like that is
>> because I don't have to put to_string implementations in headers.
>>
>> Also, it makes it very easy to integrate 3rd party types into the
>> framework. Suppose you have something like:
>>
>> class bar
>> {
>> public:
>> std::string getstr() const;
>> private:
>> ...
>> };
>>
>> and you want to integrate it into the to_string framework. All you
>> have to do is (presumably you can't alter bar itself):
>>
>> std::string to_string( bar const & x ) { return x.getstr(); }
>>
>> (note, no coupling between the above to_string overload and the
>> to_string library) and now you can do:
>>
>> bar x;
>> std::wstring s=to_wstring(x); //the library handles string->wstring
>> conversion automatically.
>
> And what if I write my own class outer_bar that owns bar and also want to be
> ostreamable? I would have to duplicate my operator<< in order to call either
> to_string or to_wstring, wouldn't I?
No.
You can define template operator<< or you can define non-template
std::string overload of operator<< only. The user would still be able
to call to_wstring, which will bind to a wstring overload if it's
available. If not, the generic to_wstring overload will call the
std::string operator<< overload, and then convert the result to
wstring.
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk