
On Wed, Feb 18, 2009 at 2:16 PM, Andrey Semashev <andrey.semashev@gmail.com> wrote:
Emil Dotchevski wrote:
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<<
How is that?
What do you mean how? You just define a template operator<< and the library-supplied to_string/to_wstring templates will bind to it, assuming the user's calls to_string or to_wstring doesn't bind to a better overload of to_string/to_wstring.
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.
But I will lose generic ostreamability for my class then.
Even that's not necessarily true, as the library can provide generic ostream template operator<< overload that's implemented in terms of to_wstring or to_string or other available operator<< overloads. I know this is tricky business, but it's tricky for the library itself. From user's point of view it's much more straight-forward: they can call to_string, to_wstring, operator<<, whatever, and the library automatically picks the best way to get it done. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode