Is there any reason for the lack of an overloaded version of to_string
receiving a string? (in which case would work as the identity)
This is useful when the argument is a template argument.
For example:
template <class T>
void f(T t)
{
auto x = "Hello " + std::to_string(t);
}
then call it with t=1, t="World", etc.
Sorry if I'm missing something too obvious.
On Fri, May 29, 2015 at 2:13 PM, Nevin Liber <ne...@eviloverlord.com> wrote:
> On 29 May 2015 at 12:06, dgutson . <daniel...@gmail.com> wrote:
>>
>> Is there any reason for the lack of an overloaded version of to_string
>> receiving a string? (in which case would work as the identity)
>> This is useful when the argument is a template argument.
>> For example:
>>
>> template <class T>
>> void f(T t)
>> {
>> auto x = "Hello " + std::to_string(t);
>> }
>>
>> then call it with t=1, t="World", etc.
>>
>> Sorry if I'm missing something too obvious.
>
>
> Perhaps to avoid an unintentional copy?
It shouldn't be necessary if the argument is const string& and the
return type is also const reference.
Having everything else return by value while this returns by const reference is a hack which is ultimately a bad design for generic code. For instance, this now returns a dangling reference:auto& r = to_string("Ok"); // okauto& s = to_string(string("Oops")); // oopsWhile you can fix the dangling reference problem with yet another overload (string to_string(string const&&)), you still have to understand the lifetime extension rules for references to use it efficiently in generic code.
For example?
We used to do this with a strinstream and all the formatting manipulators complexity.
String-to-string conversion is trivial.
What other types would you convert and with which criteria?
>
>
> Bo Persson