Boost logo

Boost :

Subject: Re: [boost] Formal Review Request: Boost.String.Convert
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2009-02-17 21:11:57


On Tue, Feb 17, 2009 at 5:18 PM, <Vladimir.Batov_at_[hidden]> wrote:
> string encrypted = boost::string::convert(naked_str) >> my_cypher;
> string decrypted = boost::string::convert(encrypted) >> my_cypher;

What's wrong with:

std::string encrypted=encrypt(naked_str);
std::string decrypted=decrypt(encrypted);

>>> My problem is that I can't think of a case when I would be writing
>>> code that doesn't "know" if it is going "to" or "from" string.
>
> Again, string-to-string (or string-from-string) comes to mind. Any
> directionality seems superfluous and I personally find the code above
> quite appropriate.

Sigh. I guess it is possible to cram any functionality that takes a
string and returns a string into convert() and >> but I wouldn't call
it appropriate.

I still can't think of a use case when I'd want to convert a
non-string to string, or string to non-string, but I don't know which
way.

>>> I've needed to convert things specifically to std::string plenty of
>>> times to justify a simple interface where to_string returns
>>> std::string. I want to be able, in a header file, to just say:
>>> struct foo;
>>> std::string to_string( foo const & );
>>> std::string to_string( foo const &, fmt );
>>> and be done with it.
>
> I hope that the suggested interface and implementation allow enough
> flexibility and simplicity without limiting one to std::string only:
>
> namespace bstring = boost::string;
>
> std::string s1 = bstring::convert(foo);
> std::string s1 = bstring::convert(foo) >> std::hex; // add formatting
> std::Wstring s1 = bstring::convert(foo, std::wtring()); // convert to wide
> string
> std::Wstring s1 = bstring::convert<std::wstring>(foo); // same as above

I was specifically concerned with the foo header, that is, how would
the foo to_string declarations look like, not how you'd call them.
Note that both to_string overloads I specified aren't templates. If we
assume that fmt is some kind of formatting string, then foo.hpp could
look like:

#include <string>

class foo;
std::string to_string( foo const & );
std::string to_string( foo const &, char const * fmt );

What would this header look like using convert()?

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