суббота, 25 марта 2017 г., 4:25:19 UTC+3 пользователь Nicol Bolas написал:
On Friday, March 24, 2017 at 7:32:50 PM UTC-4, Konstantin Tenzin wrote:
пятница, 24 марта 2017 г., 22:19:58 UTC+3 пользователь Nicol Bolas написал:
It's hard to blame developer for changing return value from string_view to string. They are both "strings", both passed by value. Compilation passed, and refactoring does not look too risky. It looks scary.
No, it's very easy to blame developers for that.
Refactoring of that nature always has to be done carefully. You cannot just change one type to another without any thought to the consequences of it. That's why refactoring in C++ is best done by creating new functions and deprecating old ones, rather than modifying the signatures of existing APIs.
Anytime you change function signatures, you are opening yourself up to all kinds of non-compatible breakage. Even changing a `const string&` to a `string` return value can break existing code.
It is very hard to agree with this point of view. I can hardly imagine so correct user code, which is so clean and simple, that would break and become unnoticed by the compiler after the changing return value from 'const string&' to 'string'.
string_view sv = object.get_string();auto sv = string_view(object.get_string()); //I want to *explicitly* convert it to a string_view.
auto ptr = object.get_string().c_str(); //I want to get the data from it.
for(auto ch: object.get_string()); //I want to iterate over the characters in the string.On 2017-03-24 23:59, Nicol Bolas wrote:
> On Friday, March 24, 2017 at 10:33:10 PM UTC-4, Konstantin Tenzin wrote:
>> суббота, 25 марта 2017 г., 4:25:19 UTC+3 пользователь Nicol Bolas написал:
>>> Anytime you change function signatures, you are opening yourself up to
>>> all kinds of non-compatible breakage. Even changing a `const string&` to a
>>> `string` return value can break existing code.
>>>
>>
>> It is very hard to agree with this point of view. I can hardly imagine so
>> correct user code, which is so clean and simple, that would break and
>> become unnoticed by the compiler after the changing return value from
>> 'const string&' to 'string'.
>
> Ironically, this very thread provides the perfect example:
>
> string_view sv = object.get_string();
>
> If `get_string` returned a `const string &`, a reference to a string stored
> in `object`, then that's perfectly functional code. If `get_string` returns
> a `string` by copy, then this is not functional code. It's well-formed, but
> using `sv` causes undefined behavior, and for the very reason that this
> thread exists.
Wait, string_view(string&&) is not deleted?! I really, really hope that
is wrong, and the above would be a hard compile error!
> for(auto ch: object.get_string()); //I want to iterate over the characters
> in the string.
I think this is okay?
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
> I don't disagree. But that's how it currently is. And changing it now would be backwards-incompatible.incompatible with what?
> That wouldn't be distinguishable from
>
> void f(string_view);
> f(string(whatever));
>
> which we presumably want to keep valid.
Ack, that's most unfortunate. It's unfortunate we (apparently?) lack the
granularity to delete functions that take or return a reference to an
object's internal state when we know that object is going to cease to
exist before we can make use of the result.